wpf 按资源中的样式指定网格列和行定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17922487/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specifying Grid column and row definition by style in resource
提问by Ryszard D?egan
There is an UserControl with the following Grid:
有一个带有以下网格的 UserControl:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
Now I have a Window and there are I would to write something like that:
现在我有一个窗口,我想写这样的东西:
<Window.Resources>
<Style TargetType="Grid">
<Setter Property="RowDefinitions">
<Value>
<RowDefinition Height="*"/>
<RowDefinition/>
</Value>
</Setter>
</Style>
</Window.Resources>
The key part, which doesn't compile is when I want to change Height from Auto to *. How to do this in legal way?
无法编译的关键部分是当我想将高度从 Auto 更改为 *. 如何以合法的方式做到这一点?
In general I have to cases. 1) The first row should stretch and the second should be fixed. 2) Vice versa. Maybe a different panel than Grid could be more relevant?
一般来说,我必须案例。1)第一排应该伸展,第二排应该固定。2)反之亦然。也许与 Grid 不同的面板可能更相关?
回答by Clemens
Grid.RowDefinitionsand Grid.ColumnDefinitionsare no dependency properties, and can't hence be set by a Style.
Grid.RowDefinitions并且Grid.ColumnDefinitions不是依赖属性,因此不能由样式设置。
You may perhaps create a dependency property FirstRowHeightin your UserControl, and bind the Heightof the first RowDefinitionto that property. Later you may set the FirstRowHeightproperty in a Style.
您可能会FirstRowHeight在您的 UserControl 中创建一个依赖属性,并将Height第一个RowDefinition属性绑定到该属性。稍后您可以FirstRowHeight在Style.
<Grid.RowDefinitions>
<RowDefinition Height="{Binding FirstRowHeight,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"/>
<RowDefinition/>
</Grid.RowDefinitions>
The property would look like this:
该属性将如下所示:
public static readonly DependencyProperty FirstRowHeightProperty =
DependencyProperty.Register(
"FirstRowHeight", typeof(GridLength), typeof(YourUserControl));
public GridLength FirstRowHeight
{
get { return (GridLength)GetValue(FirstRowHeightProperty); }
set { SetValue(FirstRowHeightProperty, value); }
}
EDIT: In order to support the simple scenario you describe at the end of your question, you may also just have an IsFirstRowFixeddependency property with a property changed callback that sets the row heights in code:
编辑:为了支持您在问题末尾描述的简单场景,您还可能只有一个IsFirstRowFixed依赖属性,该属性带有一个属性更改回调,用于在代码中设置行高:
<Grid.RowDefinitions>
<RowDefinition x:Name="row1" Height="*"/>
<RowDefinition x:Name="row2" Height="Auto"/>
</Grid.RowDefinitions>
The property:
物业:
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;
}
}
回答by Mahsh Nikam
XAML code :
XAML 代码:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1" Style="{StaticResource HeaderHeight}"</>
<Grid Grid.Row="1" Style="{StaticResource FooterHeight}"</>
</>
Styles in Resource dictionary
资源字典中的样式
<Style TargetType="Frame" x:Name="HeaderHeight">
<Setter Property="Height" Value="700"></Setter>
</Style>
<Style TargetType="Grid" x:Name="FooterHeight">
<Setter Property="Height" Value="70"></Setter>
</Style>

