I have found code like this for dynamically creating a Grid and some columns:
我找到了这样的代码,用于动态创建网格和一些列:
Grid grd = new Grid();
ColumnDefinition c = new ColumnDefinition();
c.Width = new GridLength(50, GridUnitType.Pixel);
. . .
grd.ColumnDefinitions.Add(c);
But I want to create columns with relative widths, that correspond to the "*"s used in xaml:
但是我想创建具有相对宽度的列,这些列对应于xaml中使用的“*”:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="4*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
How can I use these relative width values/ratios dynamically?
如何动态使用这些相对宽度值/比率?
1 个解决方案
#1
18
OK, I figured it out, right after posting the question:
好的,我在发布问题后想出来了:
. . .
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);
. . .
#1
18
OK, I figured it out, right after posting the question:
好的,我在发布问题后想出来了:
. . .
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);
. . .