Grid.RowDefinitions
并且Grid.ColumnDefinitions
没有依赖属性,因此不能由样式设置。
您可能会FirstRowHeight
在您的 UserControl 中创建一个依赖属性,并将Height
第一个属性绑定RowDefinition
到该属性。稍后您可以将FirstRowHeight
属性设置在Style
.
<Grid.RowDefinitions>
<RowDefinition Height="{Binding FirstRowHeight,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
<RowDefinition/>
</Grid.RowDefinitions>
该属性将如下所示:
public static readonly DependencyProperty FirstRowHeightProperty =
DependencyProperty.Register(
"FirstRowHeight", typeof(GridLength), typeof(YourUserControl));
public GridLength FirstRowHeight
{
get { return (GridLength)GetValue(FirstRowHeightProperty); }
set { SetValue(FirstRowHeightProperty, value); }
}
编辑:为了支持您在问题末尾描述的简单场景,您可能还只有一个IsFirstRowFixed
依赖属性,其中包含一个属性更改回调,用于设置代码中的行高:
<Grid.RowDefinitions>
<RowDefinition x:Name="row1" Height="*"/>
<RowDefinition x:Name="row2" Height="Auto"/>
</Grid.RowDefinitions>
物业:
public static readonly DependencyProperty IsFirstRowFixedProperty =
DependencyProperty.Register(
"IsFirstRowFixed", typeof(bool), typeof(UserControl2),
new PropertyMetadata((o, e) => ((UserControl2)o).IsFirstRowFixedChanged()));
public bool IsFirstRowFixed
{
get { return (bool)GetValue(IsFirstRowFixedProperty); }
set { SetValue(IsFirstRowFixedProperty, value); }
}
private void IsFirstRowFixedChanged()
{
if (IsFirstRowFixed)
{
row1.Height = GridLength.Auto;
row2.Height = new GridLength(1, GridUnitType.Star);
}
else
{
row1.Height = new GridLength(1, GridUnitType.Star);
row2.Height = GridLength.Auto;
}
}