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

时间:2017-04-20 09:41:24

标签: wpf icommand

我将ICommand nammed GoToSheet绑定到带有ControlTemplate的Button。

<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)? 该命令是否适用于整个按钮区域?

2 个答案:

答案 0 :(得分:1)

它是由缺乏背景引起的(按钮背景为NULL)。 请为某些颜色设置背景,例如:

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

以下是类似问题的解决方案:

Mouse event on transparent background

应该有所帮助。

答案 1 :(得分:0)

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

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

感谢macieqqq。