5

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.

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

enter image description here

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

Edit: The 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>
Douglas Gaskell
  • 9,017
  • 9
  • 71
  • 128
  • 1
    can we have some code please ? there are binding issues when check box is placed in expander... – Muds May 28 '15 at 08:10
  • 1
    Why do you need to prevent it happening? What problem is it causing? – David Arno May 28 '15 at 08:21
  • Added in the XAML. David, because it's causing the counter to count the checkbox as checked or unchecked based on it's IsChecked property when the event fires. I know there are ways to work around that, and other methods I could use to determine if it is checked or not. However that is beyond the scope of this question. – Douglas Gaskell May 28 '15 at 08:35
  • can I also have code for the parent of checkbox, seemingly its an expander as well.. – Muds May 28 '15 at 09:31
  • Sure, updated and added that in – Douglas Gaskell May 28 '15 at 17:41
  • 3
    I think it is because you have the expander as part of the **content** of the Checkbox, so any click event within the checkbox content (such as expanding the expander) will also affect the checkbox events. Try putting a horizontal stackpanel with the checkbox with the word first, and then the expander as a separate item. – Meredith Jul 16 '15 at 18:11
  • I considered that, though should the checkbox not be checked then? It's firing the event as if it was just checked or unchecked, without actually checking or unchecking the checkbox. I'll have to look into doing that. – Douglas Gaskell Jul 16 '15 at 18:59
  • Can you post the VerifyCheckBoxChecked Event handler code? – nickm Jul 24 '15 at 06:06

1 Answers1

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.

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.

<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

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.

ChronosMOT
  • 341
  • 5
  • 17