I have an app where I create a Window in WPF that the user can drag around. I’d like to limit it so it doesn’t go over the parent window’s main menu. If I handle the WindowLocationChanged event it’s fired after the change has already taken place so the only thing I can do is force the window back to some other position, which creates bad visual effects.
Is there any event I can handle before the move has actually occurred so I can incercept the window and force it to stay within certain limits? Some commercial WPF libraries support an OnMoving event which gets fired before the move actually happens on the screen. Is there anything like that in native WPF (staying within the managed code environment, if possible)?
Alternatively, is there any way to use dependency properties to set max or min x,y values? (N.B., I’m moving it, not resizing it)
Thanks in advance.
This example is a demo to show how to prevent the Window from being moved below it’s starting X/Y position…it should give you a starting point to what you want to do.
Note, it’s possible when resizing to alter the top and left position which is why the WM_SIZING message is being handled too to ensure the resize doesn’t do that.
If you don’t need that then just take out the WM_SIZING case.