[英]How to adjust control width and height according to windows width and height in WPF
嗨,我是WPF编程的新手。 我有一个窗口,其宽度和高度根据我的窗口的宽度和高度而定。
TblWind.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight; //(768)
TblWind.Width = System.Windows.SystemParameters.FullPrimaryScreenWidth; //(1024)
现在我要在此窗口中添加Grid,但要根据窗口的高度和宽度(即4行)调整其高度和宽度,每行应仅占窗口高度的5%类似地,两列的每个宽度应为窗口列的5%
<Grid.RowDefinitions>
<RowDefinition Height=".5*" ></RowDefinition>
<RowDefinition Height=".5*"></RowDefinition>
<RowDefinition Height=".5*"></RowDefinition>
<RowDefinition Height=".5*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".5*"></ColumnDefinition>
<ColumnDefinition Width=".5*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Name:"></Label>
<Label Grid.Row="1" Grid.Column="0" Content="Name:"></Label>
<Label Grid.Row="2" Grid.Column="0" Content="Name:"></Label>
<Label Grid.Row="3" Grid.Column="0" Content="Name:"></Label>
<TextBox Grid.Row="0" Grid.Column="1" ></TextBox>
<TextBox Grid.Row="1" Grid.Column="1" ></TextBox>
<TextBox Grid.Row="2" Grid.Column="1" ></TextBox>
<TextBox Grid.Row="3" Grid.Column="1" ></TextBox>
</Grid>
但是它将窗口宽度分成两部分,每部分占宽度的50%。
请帮忙。
您的比例大小只是意味着所有行高和列高都将相同。 它不是测量尺寸的百分比。
即
<RowDefinition Height="0.5*"/>
<RowDefinition Height-"0.5*"/>
只是意味着第一行的大小的50%等于第二行的大小的50%。 即,它们的高度相等。
与您说的没什么不同:
<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>
只是意味着第一行的两倍等于第二行的两倍。
如果要将网格限制为窗口高度的顶部20%(4 * 5%= 20%)和窗口宽度的最左侧10%(2 * 5%= 10%),则强制将其Grid
本身,然后就可以让行和列的大小都相等。
<Window>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="1*"/> <!-- 1/5th (20%) of the height -->
<RowDefinition Height="4*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/> <!-- 1/10th (10%) of the width -->
<ColumnDefinition Width="9*"/>
</Grid.ColumnDefinitions>
<!-- now we have a dedicated cell for your grid... -->
<Grid Grid.Row="0" Grid.Column="0">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<!-- duplicate your content here... -->
</Grid>
</Grid>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.