Expanding and minimized events expanded child expanders by increasing parent expander?

For some reason, a child Expander (placed in a StackPanel inside another Expander ), when minimized or expanded, causes the parent Expander to raise Expanded or Collapsed events.

Does anyone know why this is or how I can change it? I'm only interested in parenting events.

Here are some XAML tests:

  <Expander Header="Outer" Expanded="Expander_Expanded" Collapsed="Expander_Collapsed"> <StackPanel> <Expander Header="Inner1"> <Canvas Height="100" Width="100" Background="Blue" /> </Expander> <Expander Header="Inner2"> <Canvas Height="100" Width="100" Background="Red" /> </Expander> </StackPanel> </Expander> 

and here is the code:

  private void Expander_Expanded(object sender, RoutedEventArgs e) { MessageBox.Show("expanded"); } private void Expander_Collapsed(object sender, RoutedEventArgs e) { MessageBox.Show("collapsed"); } 

When you run this, if you disassemble the parent, you will get an "extended" mailbox, as you would expect. But when you later expand one of the children, you again get a message.

The documentation for the extended event states:

The Expanded event occurs when the IsExpanded property changes from false to true.

But explicitly, the IsExpanded property does not change on the parent expander.

Why is this happening, any ideas?

+6
source share
1 answer

These events are routed and bubble in the tree, if you want the parents to not process the event and thus respond to it, set e.Handled to true in the event handler of the child extender.

Edit: Instead of preventing the event from occurring, you can also simply limit the execution of the code in the handler to the case where the actual expander to which the handler is attached raised the event. You can do this by wrapping everything in an if block that executes if sender == e.OriginalSource .


(Woo, 10k ...)

+11
source

Source: https://habr.com/ru/post/888431/


All Articles