我已经找到这样的代码来动态创建一个网格和一些列:
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>
如何动态地使用这些相对宽度值/比例?
解决方法
好的,我想出来,发布后问题:
. . . 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); . . .