I want to make a WPF Window that behaves like a context menu.
So, for instance – when I show the wpf window, I want it to be the topmost window and from there on out, if the user clicks anything outside of that window I want the window to be hidden again.
So far I have tried quite a few techniques but the only one that I found that kinda works is shown here. It works the very first time but never again after that:
public TheWindow() { InitializeComponent(); this.Topmost = true; } void ShowMe() { this.Show(); this.CaptureMouse(); } void Window_MouseDown(object sender, MouseButtonEventArgs e) { /// TODO: Check if they clicked outside the window here! this.ReleaseMouseCapture(); this.Hide(); }
I also tried just handling the Deactivated event, but it also only works the first time for some reason.
Is PopUp not sufficient for your needs? It has a StaysOpen property that lets it act like you describe.