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

标签 wpf xaml xamarin layout grid

下面的 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>

最佳答案

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

  • 固定宽度,
  • 自动 – 列将根据需要变得尽可能宽以适合其子项,或者
  • *(星号)占用任何可用的剩余空间

* 以数字为前缀(如果未指定数字,则默认为 1)。可用空间按照前缀数的比例分配到加星标的列中。

如果你有这个定义

<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 的可用空间。

<小时/>

在您的特定情况下,网格的宽度为 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>

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

关于wpf - XAML ColumnDefinition 中 *(星号)的含义是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6956832/

相关文章:

C# - 删除 WPF DataGrid 中的右边缘行边框

wpf - 在 ItemsControl 中的每个项目周围包裹一些东西

c# mvvm绑定(bind)按钮命令参数来控制compositeCollection

wpf - 在 DynamicResource 上使用 BasedOn 样式属性

xamarin - 如何使用 Xamarin 表单中的 TabbedPage 在导航栏中添加徽章计数?

xamarin - Mandroid 错误 XA9005 : User code size, 2945919 字节,大于 131072,需要商业(或更高)许可证。

c# - 未应用自定义控件的样式

c# - WPF前要不要学Window Form?

.net - 我如何告诉 ContextMenu 将其自身相对于其控件而不是光标放置?

c# - 如何在 Xamarin Forms 中的 "re-adding"TabbedPage 子项时禁用 BottomNavigationView 切换模式?