NoWar
NoWar

Reputation: 37633

How to change size of WPF Window dynamically?

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?

Upvotes: 27

Views: 65157

Answers (3)

winscripter
winscripter

Reputation: 700

Resizing the current window is very easy. You just need to change the Width and Height properties, respectively:

private void ChangeSize()
{
// set window size to 640x480 pixels (x, y)
    this.Width = 640;
    this.Height = 480;
}

This will update the window size immediately.

Alternatively, you may also use ScrollViewer to create a scrollbar so that descendant elements fit to the window, Frame, UserControl, or anything else.

Hope my solution helps.

Upvotes: 0

Jhossep
Jhossep

Reputation: 551

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.

Upvotes: 7

Matten
Matten

Reputation: 17621

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.

Upvotes: 60

Related Questions