简体   繁体   English

复选框检查事件未触发

[英]Checkbox check event doesn't get fired

I'm stuck with this. 我坚持这一点。 I have a button with a checkbox inside in WPF. 我在WPF中有一个带有复选框的按钮。

The button in the template: 模板中的按钮:

<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>

As the checkbox is declared inside a style, in order to get a reference to that checkbox, I do from code-behind: 由于复选框是在样式内声明的,因此为了获得对该复选框的引用,我从后面的代码中进行操作:

FrameworkElement templatedControl = this.deleteButton.Template.LoadContent() as FrameworkElement;
CheckBox templatedCheck = templatedControl.FindName("checkbox") as CheckBox;

And then, I implement the method for the click event, in code-behind: 然后,我在代码背后实现了click事件的方法:

public void DeleteClick(object sender, RoutedEventArgs e)
    {
        if (!(e.OriginalSource is CheckBox))
        {
                bool isChecked = templatedCheck.IsChecked.HasValue && templatedCheck.IsChecked.Value == true;
                Debug.Print(isChecked);    // This always returns false
        }
    }

But I always get false value. 但是我总是得到false价值。

I also tried adding a RoutedEventHandler in the constructor: 我还尝试在构造函数中添加RoutedEventHandler

templatedCheck.Checked += new RoutedEventHandler(this.OnCheckChanged);
templatedCheck.Unchecked += new RoutedEventHandler(this.OnCheckChanged);

And then defined the handlers. 然后定义处理程序。 But no effect, the methods aren't called when I check the checkbox. 但是没有效果,当我选中该复选框时不会调用这些方法。

My thoughts are that it has something to do with the fact that the checkbox is inside the button and it's like when the checked event gets fired from the checkbox, it is catched by the button and doesn't arrive to my code. 我的想法是,该复选框位于按钮内部,这与从复选框触发选中事件时类似,该事件被按钮捕获,并且未到达我的代码。 Anyway, is my hypothesis, but can't wonder the solution. 无论如何,这是我的假设,但不知道解决方案。

Style for the checkbox: 复选框的样式:

<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 for the button (with the checkbox inside): 按钮的样式(内部带有复选框):

<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>

This should work: 这应该工作:

private void SaveClick(object sender, RoutedEventArgs e)
{
    if (!(e.OriginalSource is CheckBox))
    {
        Button button = (Button)sender;
        CheckBox templatedCheck = button.Template.FindName("checkbox", button) as CheckBox;
        if (templatedCheck != null)
            Debug.Print(templatedCheck.IsChecked.ToString());
    }
}

It gets a reference to the CheckBox in the template of the Button that is actually clicked, and this one is either checked or not checked. 它获得对实际单击的Button模板中CheckBox的引用,并且该CheckBox被选中或未被选中。

DataTemplate.LoadContent() creates another Button element based on the template which is not what you want. DataTemplate.LoadContent()基于模板创建另一个Button元素,这不是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM