*(星号)在WPF ColumnDefinition中的含义? [英] Meaning of * (asterisk) in a WPF ColumnDefinition?

查看:1013
本文介绍了*(星号)在WPF ColumnDefinition中的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下XAML中*(星号)的含义是什么?

 < ColumnDefinition Width =0.07 * /> 
< Grid Height =100Horizo​​ntalAlignment =Left
Margin =102,134,0,0
Name =grid1VerticalAlignment =Top
Width = 354 >
< Grid.ColumnDefinitions>
< ColumnDefinition Width =40 */>
< ColumnDefinition Width =314 */>
< /Grid.ColumnDefinitions>
< / Grid>


解决方案

当您在WPF网格中定义列时,将宽度设置为以下三种可能值之一:


  • 固定宽度

  • Auto - 列会变得尽可能的宽以适合其子元素,或者
  • * (星级)占用任何剩余空间



* 的前缀为一个数字(如果没有指定数字,则默认为1)。



如果您有这个定义



<$> p $ p> < Grid.ColumnDefinitions>
< ColumnDefinition Width =0.07 */>
< ColumnDefinition Width =0.93 */>
< /Grid.ColumnDefinitions>

第一列将获得可用总空间的7%,第二列将获得93%。另一方面,如果你有这样的定义:

 < Grid.ColumnDefinitions> 
< ColumnDefinition Width =0.07 */>
< ColumnDefinition Width =0.14 */>
< /Grid.ColumnDefinitions>

第一列将获得1/3和第二个2/3的可用空间。 p>




在你的具体情况下,网格的宽度是354,两列的比例是40和314,以下列宽:

 
第一列宽度= 40 /(40 + 314)* 354 = 40
第二库仑宽度= 314 /(40 + 314)* 354 = 314

当网格宽度不固定时,最好使用星型宽度。当网格调整大小后,列将按照星形宽度的指定按比例缩放。在你的情况下,网格的宽度是固定的,你可以很容易地使用固定宽度的列。



如果你想要一个布局,第二列是宽度的两倍第一列和第三列的宽度是您需要此定义的第一列宽度的三倍:

 < Grid.ColumnDefinitions> 
< ColumnDefinition Width =*/>
< ColumnDefinition Width =2 */>
< ColumnDefinition Width =3 */>
< /Grid.ColumnDefinitions>

如果网格的总宽度为300,则可以获得列宽50,100和150。网格的总宽度为600,您可以获得100,200和300的列宽。依此类推。


What is the meaning of * (asterisk) in the XAML below?

<ColumnDefinition Width="0.07*"/>
<Grid Height="100" HorizontalAlignment="Left" 
      Margin="102,134,0,0" 
      Name="grid1" VerticalAlignment="Top" 
      Width="354">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="40*" />
        <ColumnDefinition Width="314*" />
    </Grid.ColumnDefinitions>
</Grid>

解决方案

When you define a column in a WPF grid you can set the width to one of three possible values:

  • A fixed width,
  • Auto – column will become as wide as necessary to fit its children, or
  • * (star) take up any available remaining space

The * is prefixed by a number (default is 1 if no number is specified). The available space is divided among the starred columns in proportion to the prefix number.

If you have this definition

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="0.07*"/>
  <ColumnDefinition Width="0.93*"/>
</Grid.ColumnDefinitions>

The first column will get 7% of the total space available and the second column would get 93%. On the other hand if you had this definition:

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="0.07*"/>
  <ColumnDefinition Width="0.14*"/>
</Grid.ColumnDefinitions>

The first column would get 1/3 and the second 2/3 of the available space.


In your specific case where the width of the grid is 354 and the proportions of the two columns are 40 and 314 you get the following column widths:

First column width = 40/(40 + 314)*354 = 40
Second coulmn width = 314/(40 + 314)*354 = 314

The star width is best used when the width of the grid isn't fixed. When the grid is resized the columns will then scale proportionally as specified by the star widths. In your case the width of the grid is fixed and you could just as easily have used fixed width columns.

If you want a layout where the second column is double the width of the first and the third column is triple the width of the first you need this definition:

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="*"/>
  <ColumnDefinition Width="2*"/>
  <ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>

If the total width of the grid is 300 you get column widths 50, 100 and 150. If the total width of the grid is 600 you get column widths 100, 200 and 300. And so on.

这篇关于*(星号)在WPF ColumnDefinition中的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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