带有 ControlTemplate 的 WPF 按钮上的命令不适用于整个区域

Command on WPF Button with ControlTemplate doesn't work on the entire area

我正在将名为 GoToSheet 的 ICommand 绑定到带有 ControlTemplate 的按钮。

<Button Margin="10 10 10 10" Command="{Binding GoToSheet}">
    <Button.Template>
        <ControlTemplate>
            <DockPanel Width="840">
                <Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="5"></Border>
                <Border DockPanel.Dock="Top" BorderBrush="Black" BorderThickness="2"></Border>
                <Border DockPanel.Dock="Right" BorderBrush="Black" BorderThickness="2"></Border>
                <Border DockPanel.Dock="Bottom" BorderBrush="Black" BorderThickness="2"></Border>
                <DockPanel DockPanel.Dock="Left">
                    <StackPanel Orientation="Vertical" Margin="20 10 0 10">
                        <StackPanel Orientation="Horizontal">
                            <StackPanel Orientation="Vertical" Margin="0 0 0 10">
                                <TextBlock Text="{Binding Client}" FontSize="30" FontWeight="Bold" MaxWidth="480"></TextBlock>
                                <TextBlock Text="{Binding Lieu}" FontSize="22" MaxWidth="480"></TextBlock>
                            </StackPanel>
                            <Image Height="35" Width="45" Margin="7 3 0 0" VerticalAlignment="Top" Source="/Resource/Image/CHC.png"></Image>
                        </StackPanel>
                        <StackPanel Orientation="Vertical" VerticalAlignment="Bottom" Margin="20 0 0 0">
                            <TextBlock Text="{Binding MarqueEngin}" FontSize="26" FontWeight="Bold"></TextBlock>
                            <TextBlock Text="{Binding ModeleEngin}" FontSize="19" FontWeight="Bold"></TextBlock>
                        </StackPanel>
                    </StackPanel>
                </DockPanel>
                <DockPanel DockPanel.Dock="Right" Margin="0 10 20 10">
                    <TextBlock DockPanel.Dock="Top" Text="{Binding DateIntervention, StringFormat=dd/MM/yyyy}" FontSize="30" HorizontalAlignment="Right"></TextBlock>
                    <TextBlock DockPanel.Dock="Bottom" Text="{Binding NumeroEngin}" VerticalAlignment="Bottom" FontSize="30" FontWeight="Bold" HorizontalAlignment="Right"></TextBlock>
                </DockPanel>
            </DockPanel>
        </ControlTemplate>
    </Button.Template>
</Button>

为什么该命令仅适用于成功区域(参见 image)? 有没有针对整个按钮区域的命令的解决方案?

是缺少背景造成的(按钮背景为NULL)。 请设置一些颜色的背景,例如:

<Button Margin="10 10 10 10" Background="Transparent" Command="{Binding GoToSheet}">

类似问题的解决方法如下:

Mouse event on transparent background

应该会有帮助。

透明背景必须在 DockPanel 上代替 Button。

<Button Margin="10 10 10 10" Command="{Binding GoToSheet}">
        <Button.Template>
            <ControlTemplate>
                <DockPanel Width="840" Background="Transparent">

感谢macieqqq