Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change size of WPF Window dynamically?

Tags:

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?

like image 656
Friend Avatar asked Jan 25 '12 13:01

Friend


People also ask

How do I resize a control in WPF?

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.

Can you resize with grip?

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.


2 Answers

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.

like image 111
Matten Avatar answered Sep 19 '22 05:09

Matten


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.

like image 30
Jhossep Augusto Popayán Avila Avatar answered Sep 21 '22 05:09

Jhossep Augusto Popayán Avila