Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto close expander

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.

like image 932
Dimitris Avatar asked May 24 '12 13:05

Dimitris


1 Answers

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;
}
like image 158
LPL Avatar answered Oct 24 '22 10:10

LPL