I have used an expander control to create some sort of slide-out panel which holds some filtering options. The users have the option to 'pin' this expander to the view. All went well but now I would like to know how I can get the expander to auto close when the user clicks somewhere out of the expander. I tried the LostFocus and several other events but to no avail. Which event can inform me when the user has clicked somewhere else?
(For the moment I'm just using a timer which closes the expander after +- 2 seconds after the mouse left the expander but I would prefer the other solution)
Thanks in advance.
Assuming your Expander
is named "yourExpander" you could do this:
<Window PreviewMouseDown="Window_PreviewMouseDown"
If e.OriginalSource
is not a descendant of your expander close your expander:
private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
Visual visual = e.OriginalSource as Visual;
if (!visual.IsDescendantOf(yourExpander))
yourExpander.IsExpanded = false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With