Usercontrol内的按钮在WPF中无法正常触发

时间:2015-11-07 03:32:08

标签: c# wpf xaml

我有一个图像按钮类型的用户控件,它有两个文本块和一个图像内部按钮,但按钮只有当我点击文本块或图像部分时才会点击。只是为了快速调试我已经将按钮的光标设置为Hand,奇怪的是它仅在鼠标位于texblocks / Image上时显示手形光标,而不是按钮的任何部分。这是我的代码

             Name="Tiles" Background="Green"
         d:DesignHeight="300" d:DesignWidth="300" Margin="10,10,10,10">
<UserControl.Resources>
    <BooleanToVisibilityConverter x:Key="booleanVisibleConverter" />
    <!-- This style is used for buttons, to remove the WPF default 'animated' mouse over effect -->
    <Style x:Key="ImgBtnStyle" TargetType="{x:Type Button}">
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate x:Name="ImgBtnStyle" TargetType="{x:Type Button}">
                    <Border Name="border" 
                        BorderThickness="1"
                        Padding="0" 
                        BorderBrush="DarkGray" 
                        CornerRadius="1" 
                        Background="{TemplateBinding Background}">
                        <ContentPresenter  />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="BorderBrush" Value="White" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<Button Cursor="Hand" Style="{StaticResource ImgBtnStyle}" Command="{Binding TestClick, ElementName=Tiles}" CommandParameter="{Binding ElementName=Tiles, Path=TestName}" >
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/>
            <RowDefinition Height="2*"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="{Binding ElementName=Tiles, Path=ResultValue}" Style="{StaticResource txtblck}" Grid.Column="1" Grid.Row="1"/>
        <TextBlock Text="{Binding ElementName=Tiles, Path=TestName}" Style="{StaticResource txtblck}" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" VerticalAlignment="Bottom" TextAlignment="Left" Padding="5,0,0,5"/>
        <Image Source="/resources/Icons/tickBlack.png" Grid.Column="2" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" 
               Visibility="{Binding Path= TestDone, ElementName=Tiles, Converter={StaticResource booleanVisibleConverter}}"/>
    </Grid>
</Button>

1 个答案:

答案 0 :(得分:1)

将网格背景设置为透明,如下所示:

<Button Cursor="Hand" Style="{StaticResource ImgBtnStyle}" Command="{Binding TestClick, ElementName=Tiles}" CommandParameter="{Binding ElementName=Tiles, Path=TestName}" >
        <Grid Background="Transparent">
            ...
 </Button>

我做了以上修改,它对我来说很好。