开发者

Resize WPF application

开发者 https://www.devze.com 2023-04-04 17:58 出处:网络
Have tried searching google but I\'m struggling to find an answer to this. I have a basic WPF 开发者_StackOverflow社区application with a few controls on. When I maximise the application the controls

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

I have a basic WPF 开发者_StackOverflow社区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


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"/>


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.


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:

  • Layout containers
  • Data Binding


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


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号