如何在WPF中最大化窗口时调整控件大小 [英] How to resize the controls size when the window is Maximized in WPF

查看:616
本文介绍了如何在WPF中最大化窗口时调整控件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个WPF窗口和窗口上的多个控件说,4个按钮,标签,2个列表框等,当MAximize窗口,大小并且控件的位置没有调整大小。



当窗口最大化时,如何调整所有控件以调整位置和调整大小。



Plz帮帮我。

解决方案

我建​​议您使用Viewbox作为父布局容器,自动重新调整所有内容的大小。您可以通过将所有控件包装在Viewbox中来执行此操作,例如:



 <  窗口  

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x < span class =code-keyword> = http://schemas.microsoft.com/winfx/2006/xaml

......

x:Class = ChartTesting.MainPage

宽度 = 640 高度 = 480 >

< Viewbox >
< 网格 >
......
< 网格 >
< / Viewbox >

< / Window >


您可能需要使用网格布局或其他来在设计中使控件流畅。要将控件添加到网格布局面板,只需将声明放在网格的开始和结束标记之间。请记住,行和列定义必须限制子控件的任何定义。

网格布局面板提供两个附加属性Grid.Column和Grid.Row来定义控件的位置。作为示例,您可以尝试使用以下给定代码。如果按照百分比计算,你只记得100%意味着1.所以如果你有3个控件而你想把它放在三列中那么你可以设计它的宽度为.33 *为所有





 <  网格 >  
< Grid.RowDefinitions >
< RowDefinition 高度 = 自动 / >
< RowDefinition 高度 = 自动 / >
< RowDefinition 高度 = * / >
< RowDefinition 高度 = 28 / >
< / Grid.RowDefinitions >
< Grid.ColumnDefinitions >
< ColumnDefinition 宽度 = 自动 / >
< ColumnDefinition 宽度 = 200 / > ;
< / Grid.ColumnDefinitions >
< 标签 Grid.Row = 0 Grid.Column = 0 内容 < span class =code-keyword> = 名称: / >
< 标签 Grid.Row = 1 Grid.Column = 0 内容 = 电子邮件: / >
< 标签 Grid.Row = 2 Grid.Column = 0 内容 = 评论: / >
< TextBox Grid.Column = 1 Grid.Row = 0 < span class =code-attribute>保证金 = 3 / >
< TextBox Grid.Column = 1 Grid.Row = 1 保证金 = 3 / >
< TextBox Grid.Column = 1 Grid.Row = 2 保证金 < span class =code-keyword> = 3 / >
< 按钮 Grid.Column = 1 Grid.Row = 3 < span class =code-attribute> Horizo​​ntalAlignment =

MinWidth = 80 保证金 = < span class =code-keyword> 3 内容 = 发送 / >
< ; / Grid >


 <   div  >  
< / div >


Hello,

I have a WPF window and Multiple controls on the window say, 4buttons, labels, 2 listboxes etc. when the MAximize the window, the size and positions of the controls are not resizing.

How can i make all the controls to adjust position and resize when the window is maximized.

Plz help me out.

解决方案

I will suggest that you use Viewbox as your parent Layout Container which automatically re-size all contents. You can do this by just wrapping all controls inside the Viewbox like:

<Window

	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

	......

	x:Class="ChartTesting.MainPage"

	Width="640" Height="480">

   <Viewbox>
      <Grid>
      ......
      <Grid>
   </Viewbox>

</Window>


You might need to use grid layout or other to make the controls fluid in Design. To add controls to the grid layout panel just put the declaration between the opening and closing tags of the Grid. Keep in mind that the row- and columndefinitions must precced any definition of child controls.
The grid layout panel provides the two attached properties Grid.Column and Grid.Row to define the location of the control. As an Example you might try with the following given code. If you count in terms of percentage you just remember that 100% means 1. So if you have 3 controls and you want to put it in three column then you might design its width as .33* for all


<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
        <RowDefinition Height="28" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="200" />
    </Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="Name:"/>
    <Label Grid.Row="1" Grid.Column="0" Content="E-Mail:"/>
    <Label Grid.Row="2" Grid.Column="0" Content="Comment:"/>
    <TextBox Grid.Column="1" Grid.Row="0" Margin="3" />
    <TextBox Grid.Column="1" Grid.Row="1" Margin="3" />
    <TextBox Grid.Column="1" Grid.Row="2" Margin="3" />
    <Button Grid.Column="1" Grid.Row="3" HorizontalAlignment="Right"

            MinWidth="80" Margin="3" Content="Send"  />
</Grid>


<div>
</div>


这篇关于如何在WPF中最大化窗口时调整控件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆