在 WPF 中以编程方式设置带有 * 的网格列的宽度 [英] Programmatically setting the width of a grid column with * in WPF
本文介绍了在 WPF 中以编程方式设置带有 * 的网格列的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式配置 wpf 网格.
I want to programmatically configure a wpf grid.
我希望能够设置一个包含 2 列的网格,第一列占用 20% 的可用空间,第二列占用 80%.在 xaml 中,我会使用 * 运算符,但我不知道如何以编程方式执行此操作.
I want to be able to set a grid with 2 columns, the first taking up 20% of available space, the second taking up 80%. In xaml I would use the * operator but I cant work out how to do this programmatically.
在 Xaml 中我会这样做:
In Xaml I would do:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition width="20*" />
<ColumnDefinition width="80*" />
</Grid>
在我想做的代码中:
Grid grid = new Grid();
grid.ColumnDefinitions.Add( new ColumnDefinition(20*) );
grid.ColumnDefinitions.Add( new ColumnDefinition(80*) );
请有人建议.
推荐答案
Grid grid = new Grid();
ColumnDefinition c1 = new ColumnDefinition();
c1.Width = new GridLength(20, GridUnitType.Star);
ColumnDefinition c2 = new ColumnDefinition();
c2.Width = new GridLength(80, GridUnitType.Star);
grid.ColumnDefinitions.Add(c1);
grid.ColumnDefinitions.Add(c2);
这篇关于在 WPF 中以编程方式设置带有 * 的网格列的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文