wpf XAML ColumnDefinition 中 *(星号)的含义是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6956832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:01:36  来源:igfitidea点击:

What's the meaning of * (asterisk) in XAML ColumnDefinition?

wpfxamlxamarinlayoutgrid

提问by Shashank

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

下面的 XAML 中 *(星号)是什么意思?

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

回答by Martin Liversage

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

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

  • A fixed width,
  • Auto– column will become as wide as necessary to fit its children, or
  • *(star) take up any available remaining space
  • 固定宽度,
  • Auto– 列将变得尽可能宽以适合其子项,或
  • *(star) 占用任何可用的剩余空间

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.

*由多个前缀(如果没有指定数目默认值为1)。可用空间按前缀编号的比例在带星号的列中分配。

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:

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

<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.

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



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:

在网格宽度为 354 且两列的比例为 40 和 314 的特定情况下,您将获得以下列宽:

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.

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

回答by Dean Chalk

Its 0.07 ratio to any other star-width column - i.e. if another ColomnDefinition has a Width of 0.14 then that column is double the width = its all about rations

它与任何其他星形宽度列的比率为 0.07 - 即,如果另一个 ColomnDefinition 的宽度为 0.14,则该列的宽度是其两倍 = 其所有关于配给

回答by Jakub

It creates column sizes using ratios. If you had another definition like <ColumnDefinition Width="0.03*"/>the first column would take up 70% of space and the second one would take up 30%.

它使用比率创建列大小。如果您有另一个定义,例如<ColumnDefinition Width="0.03*"/>第一列将占用 70% 的空间,第二列将占用 30%。

回答by H.B.

[..] a value that is expressed as a weighted proportion of available space.

[..] 一个值,表示为可用空间的加权比例。