读书人

WPF请教Grid的列宽的默认值

发布时间: 2013-08-04 18:26:16 作者: rapoo

WPF,请问Grid的列宽的默认值
Grid的列宽、行高是一个GridLength结构吧,默认值是"*"吧,可是查看GridLength.GridUnitType属性(MSDN),默认值明明是Auto的啊
,这是怎么回事?
[解决办法]
不一定的,比如一个GRID,这表示是平分三行,当然整个grid你也可以设置大小的。
<Grid >
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
</Grid>
而如下,表示row中如果没其他有高度的控件,则改行高度就为0了
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="atuo"></RowDefinition>
<RowDefinition Height="atuo"></RowDefinition>
<RowDefinition Height="atuo"></RowDefinition>
</Grid.RowDefinitions>


</Grid>
[解决办法]
一切以MSDN为主,默认值都是Auto的
[解决办法]
没想到这种细节Lz也注意到了。我这以列来举例。
首先希望Lz明白的是列宽是ColumnDefintion.Width 属性。
请注意“备注”中的说明。

现在来说说GridLength结构,它有两个构造函数(GridLength(Double),GridLength(Double,GridUnitType))和一个GridUnitType. 那么LZ所说的GridUnitType的默认值是用在第一个构造函数的,即,GridLength在初始化时没有指定GridUnitType属性,这时GridLength.GridUnitType=GridUnitType.Auto

咱们可以理解为width是WPF在隐藏文件中以GridLength(Double,GridUnitType)这种方式初始化的GridLength,即,wpf已为width属性预设好了GridUnitType值。 在这即为GridUnitType.Star

具体验证代码:
//我在XAML定义了名为column1的ColumnDefinition
//未使用GridLength(Double,GridUnitType)指定GridUnitType
GridLength gridLength1=new GridLength();
GridLength gridLength2 = column1.Width;
//运行后gridUnitType1的值为GridUnitType.star,在XAML中为*
GridUnitType gridUnitType2 = gridLength2.GridUnitType;
//运行后gridUnitType1的值为GridUnitType.Auto
GridUnitType gridUnitType1 = gridLength1.GridUnitType;

读书人网 >C#

热点推荐