WPF。防止按钮内的复选框触发点击事件

时间:2018-09-19 09:16:23

标签: wpf xaml

我在WPF中有一个带有复选框的按钮。我希望能够在不触发按钮的点击事件的情况下选中复选框

我的问题是,当前,无论我按下复选框本身还是其标签,都会触发click事件,这是我不希望的行为。我只想在红色区域中的某处按一下即可引发点击事件。

enter image description here

有办法防止这种情况吗?

现在这是我的代码:

复选框的样式:

<Style x:Key="FilledCheckBox" TargetType="{x:Type CheckBox}">
    <Setter Property="Foreground" Value="Gray"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CheckBox}"> 
                <BulletDecorator Background="Transparent">
                    <BulletDecorator.Bullet>
                        <Border x:Name="Border"
                                Width="15"
                                Height="15"
                                CornerRadius="1"
                                BorderThickness="1">
                            <Border.BorderBrush>
                                <SolidColorBrush Color="Gray"/>
                            </Border.BorderBrush>
                            <Border.Background>
                                <SolidColorBrush Color="White"/>
                            </Border.Background>
                        </Border>
                    </BulletDecorator.Bullet>
                    <ContentPresenter Margin="5,0,0,0"
                                      VerticalAlignment="Top"
                                      HorizontalAlignment="Left"
                                      RecognizesAccessKey="True" />
                </BulletDecorator>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter Property="Background" TargetName="Border" Value="Green"/>
                        <Setter Property="BorderBrush" TargetName="Border" Value="Green"/>
                        <Setter Property="Foreground" Value="Green"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

按钮的样式(带有复选框):

<Style x:Key="RoundCornerButtonWithCheck" TargetType="{x:Type Button}">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Width" Value="120"/>
        <Setter Property="Height" Value="30"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Button.Effect">
            <Setter.Value>
                <DropShadowEffect Color="Black" Direction="280" ShadowDepth="2" BlurRadius="5" Opacity="0.2" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid x:Name="grid">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="120"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Border x:Name="border" CornerRadius="15" BorderBrush="Black" BorderThickness="0"  Background="{TemplateBinding Background}">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              TextElement.FontWeight="SemiBold">
                            </ContentPresenter>
                        </Border>

                        <CheckBox x:Name="checkbox" Grid.Column="0" VerticalAlignment="Center" Margin="20,0,0,0" Style="{StaticResource FilledCheckBox}"></CheckBox>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <SolidColorBrush Color="Gold" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF33962B"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" TargetName="grid" Value="0.25"/>
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

模板中的按钮:

<Button Name="deleteButton" Style="{StaticResource RoundCornerButtonWithCheck}" Content="{Binding Source={x:Static localization:Resources.Delete}, Converter={StaticResource CharacterCasingConverter}}" VerticalAlignment="Center" HorizontalAlignment="Left" Background="Red" Foreground="White" Margin="80,0,80,30" Click="DeleteClick"></Button>

1 个答案:

答案 0 :(得分:1)

  

有办法防止这种情况吗?

您不能真正阻止内置Button控件本身在不取消PreviewMouseLeftButtonDown事件的情况下处理点击,但是您可以实现事件处理程序来忽略对CheckBox的点击:

private void SaveClick(object sender, RoutedEventArgs e)
{
    if (!(e.OriginalSource is CheckBox))
    {
        //handle click outside the CheckBox...
    }
}