用户控件内的按钮在WPF中未正确启动

本文关键字:启动 WPF 控件 按钮 用户 | 更新日期: 2023-09-27 18:20:53

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

             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>

用户控件内的按钮在WPF中未正确启动

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

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

我做了以上的更改,对我来说效果很好。