user589195
user589195

Reputation: 4250

Resize WPF application

Have tried searching google but I'm struggling to find an answer to this.

I have a basic WPF application with a few controls on. When I maximise the application the controls on the screen stay the same size and I get a lot of wasted space.

Is there a way to get the controls to grow and shrink dynamically with the size of the main window?

Kind Regards

Ash

Upvotes: 2

Views: 6007

Answers (6)

user3456909
user3456909

Reputation: 81

Use a resizable control like Grid and put all the controls in Rows/Columns. Also set HorizontalAlignment to stretch for each control.

Upvotes: 0

Jan K.
Jan K.

Reputation: 2582

this is no problem. The behavior of your controls depends on the container you use and the Horizontal and Vertical Alignment. For Example, if you use a Grid, a TextBox and a Button with Horizontal and Vertical Alignment: Stretch and width and height auto, the Textfield and Button will grow and shrink dynamically.

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication.MainWindow"
x:Name="Window"
Title="MainWindow"
Width="640" Height="480">

<Grid x:Name="LayoutRoot">
            <TextBox Margin="8,8,8,104.96" TextWrapping="Wrap" Text="TextBox" Height="auto" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            <Button Content="Button" Margin="8,0,8,8" VerticalAlignment="Bottom" Height="92.96" Width="auto" HorizontalAlignment="Stretch"/>

</Grid>

Upvotes: 1

MichaelS
MichaelS

Reputation: 7123

Don't set a fixed Height and Width properties for your controls. Instead set, horizontal and vertical alignment to stretch. And make sure your controls are contained inside an appropriate layout panel.

For example-

Fixed size grid:

<Grid Background="Red" Width="50" Height="50"/>

Dynamically expending grid:

<Grid Background="Red" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>

Upvotes: 4

Alexander Molodih
Alexander Molodih

Reputation: 1936

You can use content decorator that can stretch and scale a single child to fill the available space - Viewbox .

http://www.wpftutorials.com/2011/04/wpf-viewbox.html

Upvotes: 1

Steven Jeuris
Steven Jeuris

Reputation: 19130

Where there's a will, there's a way. You will have to do some work yourself however, WPF can't automagically decide for you exactly how you want the resizing to be done.

Some relevant sources:

Upvotes: 1

Davide Piras
Davide Piras

Reputation: 44595

what you need is not resize, it's called scaling (or zooming) ;-)

Read this article; the task is very trivial with WPF.

(used to be much more complicated in Windows Forms...).

UI Scaling (UI Zooming) with WPF

basically all you need to add to the XAML is this:

 <ScaleTransform 
            CenterX="0" CenterY="0"
            ScaleX="{Binding ElementName=uiScaleSlider,Path=Value}"
            ScaleY="{Binding ElementName=uiScaleSlider,Path=Value}"
        />

after that you can use mouse wheel or a slider or any other way (like in your case detect form maximized), to modify the Value of the ScaleTransform.

Upvotes: 2

Related Questions