我找到了这样的代码,用于动态创建网格和一些列:
Grid grd = new Grid();
ColumnDefinition c = new ColumnDefinition();
c.Width = new GridLength(50, GridUnitType.Pixel);
. . .
grd.ColumnDefinitions.Add(c);
但我想创建具有相对宽度的列,这些列对应于xaml中使用的“*”:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
如何动态使用这些相对宽度值/比率?
答案 0 :(得分:19)
好的,我在发布问题之后就明白了:
. . .
ColumnDefinition c1 = new ColumnDefinition();
c1.Width = new GridLength(1, GridUnitType.Star);
ColumnDefinition c2 = new ColumnDefinition();
c2.Width = new GridLength(4, GridUnitType.Star);
ColumnDefinition c3 = new ColumnDefinition();
c3.Width = new GridLength(1, GridUnitType.Star);
ColumnDefinition c4 = new ColumnDefinition();
c4.Width = new GridLength(3, GridUnitType.Star);
ColumnDefinition c5 = new ColumnDefinition();
c5.Width = new GridLength(1, GridUnitType.Star);
. . .
grd.ColumnDefinitions.Add(c1);
. . .