1.) Subscribe to the window size event in the code behind:
this.SizeChanged += OnWindowSizeChanged;
2.) Use the SizeChangedEventArgs 'e' object to get the sizes you need:
protected void OnWindowSizeChanged(object sender, SizeChangedEventArgs e) { double newWindowHeight = e.NewSize.Height; double newWindowWidth = e.NewSize.Width; double prevWindowHeight = e.PreviousSize.Height; double prevWindowWidth = e.PreviousSize.Width; }
Keep in mind that this is a very common case, you CAN (you also cannot) to do some checks to make sure you have values โโof size 0.
I used this to dynamically resize the list window when changing the main window. In fact, all I wanted was this control height to change the same value as the window height, so its parent panel looks consistent in all window changes.
Here is the code for this, a more specific example:
NOTE. I have a private instance instance called "resizeMode" that is set to 0 in the window code constructor behind.
Here is the OnWindowSizeChanged event handler:
protected void OnWindowSizeChanged (object sender, SizeChangedEventArgs e) { if (e.PreviousSize.Height != 0) { if (e.HeightChanged) { double heightChange = e.NewSize.Height - e.PreviousSize.Height; if (lbxUninspectedPrints.Height + heightChange > 0) { lbxUninspectedPrints.Height = lbxUninspectedPrints.Height + heightChange; } } } prevHeight = e.PreviousSize.Height; }
source share