选中扩展器时,复选框已选中且未选中的事件被触发

时间:2022-07-20 16:07:21

I just noticed this issue, whenever I select an expander whose parent is a checkbox it triggers the checked/unchecked events even though the checkbox is not checked.

我刚刚注意到这个问题,每当我选择一个父级是复选框的扩展器时,它会触发已检查/未选中的事件,即使未选中该复选框也是如此。

Here is a .gif that shows it occurring. The number in the upper right represents the number of checkboxes checked, I set to to just count up and down whenever a box is checked or unchecked. The counter itself is not the worry here though, just a way to show it happening.

这是一个显示它发生的.gif文件。右上角的数字表示选中的复选框的数量,我设置为只在选中或取消选中框时向上和向下计数。然而,柜台本身并不是担心,只是表明它正在发生的一种方式。

For each checkbox their Checked and UnChecked events point to a method of my choosing.

对于每个复选框,它们的Checked和UnChecked事件指向我选择的方法。

选中扩展器时,复选框已选中且未选中的事件被触发

What is causing this to happen, and how do I go about preventing it?

造成这种情况的原因是什么,我该如何防止这种情况发生?

Edit: The XAML

编辑:XAML

            <Expander Loaded="VerifyExpanderLoaded" ExpandDirection="Down" IsExpanded="True" FontSize="14" FontWeight="Bold" Margin="5" BorderThickness="1" BorderBrush="#FF3E3D3D">
            <Expander.Header>
                <DockPanel RenderTransformOrigin="0.5,0.5" LastChildFill="False" Margin="0" HorizontalAlignment="Stretch">
                    <TextBlock Text="Verify Caller and Account" DockPanel.Dock="Left" VerticalAlignment="Center" Margin="0"/>
                    <Button DockPanel.Dock="Right" Margin="1,0" Click="VerifyUncheckAll">
                        <TextBlock Text="Uncheck All" Margin="1,0" FontSize="12" FontWeight="Normal"/>
                    </Button>
                    <Button DockPanel.Dock="Right" Margin="1,0" Click="VerifyCheckAll">
                        <TextBlock Text="Check All" FontSize="12" FontWeight="Normal" Margin="1,0"/>
                    </Button>
                    <TextBlock x:Name="VerifyCheckboxCount" Text="0/0" DockPanel.Dock="Right" VerticalAlignment="Center" FontSize="16" FontWeight="Bold" Margin="0,0,10,0"/>
                </DockPanel>
            </Expander.Header>
            <ListBox Background="{x:Null}" BorderBrush="{x:Null}">
                <CheckBox x:Name="Authentication_CallerName_Checkbox" HorizontalAlignment="Center" Margin="10,5,0,0" Grid.ColumnSpan="2" FontSize="12" VerticalAlignment="Center" Checked="VerifyCheckBoxChecked" Unchecked="VerifyCheckBoxChecked">
                    <StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <TextBlock Text="Caller's Name" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="14"/>
                        <StackPanel>
                            <Expander ExpandDirection="Right" Margin="5,2,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="14">
                                <TextBlock Text="Obtain Callers Name " HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" FontWeight="Normal" FontStyle="Italic">
                                <Hyperlink x:Name="AuthenticationWikiLink" NavigateUri="[Redacted]" RequestNavigate="ExternalLinks">
                                    Link
                                </Hyperlink>
                                </TextBlock>
                            </Expander>
                        </StackPanel>
                    </StackPanel>
                </CheckBox>

1 个解决方案

#1


0  

What is causing this to happen?

是什么导致这种情况发生?

I don't know. I'm guessing it happens because of routed events. You probably could prevent this from happening, if you set the eventArgs as handled in the right event.

我不知道。我猜它是因为路由事件而发生的。如果将eventArgs设置为在正确的事件中处理,则可能可以防止这种情况发生。

How do I go about preventing it?

我该如何预防呢?

First here is the XAML I used. It's based on a simplified version of the one you posted.

首先是我使用的XAML。它基于您发布的简化版本。

<CheckBox x:Name="checkbox" Checked="VerifyCheckBoxChecked" Unchecked="VerifyCheckBoxChecked">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Test:" VerticalAlignment="Center"/>
        <StackPanel>
            <Expander ExpandDirection="Right" Margin="5,2,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="14" MouseEnter="Expander_MouseEnter" MouseLeave="Expander_MouseLeave">
                <TextBlock Text="Open" VerticalAlignment="Center"/>
            </Expander>
        </StackPanel>
    </StackPanel>
</CheckBox>

And here is the .cs

这是.cs

public partial class MainWindow : Window
{
    bool mouseOverExpander = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void VerifyCheckBoxChecked(object sender, RoutedEventArgs e)
    {
        if (!mouseOverExpander)
        {
            if (true) ;
        }
    }

    private void Expander_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        mouseOverExpander = true;
    }

    private void Expander_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        mouseOverExpander = false;
    }
}

This doesn't prevent the unwanted VerifyCheckBoxChecked calls, but it filters them out.

这不会阻止不需要的VerifyCheckBoxChecked调用,但会将其过滤掉。

#1


0  

What is causing this to happen?

是什么导致这种情况发生?

I don't know. I'm guessing it happens because of routed events. You probably could prevent this from happening, if you set the eventArgs as handled in the right event.

我不知道。我猜它是因为路由事件而发生的。如果将eventArgs设置为在正确的事件中处理,则可能可以防止这种情况发生。

How do I go about preventing it?

我该如何预防呢?

First here is the XAML I used. It's based on a simplified version of the one you posted.

首先是我使用的XAML。它基于您发布的简化版本。

<CheckBox x:Name="checkbox" Checked="VerifyCheckBoxChecked" Unchecked="VerifyCheckBoxChecked">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Test:" VerticalAlignment="Center"/>
        <StackPanel>
            <Expander ExpandDirection="Right" Margin="5,2,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Right" FontSize="14" MouseEnter="Expander_MouseEnter" MouseLeave="Expander_MouseLeave">
                <TextBlock Text="Open" VerticalAlignment="Center"/>
            </Expander>
        </StackPanel>
    </StackPanel>
</CheckBox>

And here is the .cs

这是.cs

public partial class MainWindow : Window
{
    bool mouseOverExpander = false;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void VerifyCheckBoxChecked(object sender, RoutedEventArgs e)
    {
        if (!mouseOverExpander)
        {
            if (true) ;
        }
    }

    private void Expander_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        mouseOverExpander = true;
    }

    private void Expander_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        mouseOverExpander = false;
    }
}

This doesn't prevent the unwanted VerifyCheckBoxChecked calls, but it filters them out.

这不会阻止不需要的VerifyCheckBoxChecked调用,但会将其过滤掉。