Let's say we show some WPF Window and comes moment when we have to show some extra panel at the bottom.
What I want to do is to increase WPF Window size and center it again.
Any clue or samples?
Well, it's fairly simple to do. On the window resize event handler, calculate how much the window has grown/shrunk, and use that fraction to adjust 1) Height, 2) Width, 3) Canvas. Top, 4) Canvas. Left properties of all the child controls inside the canvas.
The CanResizeWithGrip option is similar to CanResize, but the lower right corner of the window shows a little “grip” icon indicating that you can “grab” the window here to resize it. The NoResize option creates a window that can't be resized, minimized or maximized.
You can programmatically change the size and location of the window, just set the appropriate Width and Height values for size and Top and Left for location. But it's even easier.
Following this page you get
<Window x:Class="SizingTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Width="Auto" Height="Auto" SizeToContent="WidthAndHeight">
to automatically adapt the window size to the content, and with the help of this link you can center the window again after the size was changed.
if you want to resize at a specific size you can do the following:
If you want to resize the main window just write the following code.
Application.Current.MainWindow.Height = 420;
If you want to resize a new window other than the main window just write the following code in the .cs file of the new window.
Application.Current.MainWindow = this; Application.Current.MainWindow.Width = 420;
Hope it helps.
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