问题描述
我有一个网格,它的行需要根据视图模型动态调整大小.我想做类似以下的事情:
I have a grid whose rows need to be resized dynamically based on the view model. I'd like to do something like the following:
<RowDefinition Height="2*">
<RowDefinition.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ShowSection}" Value="True">
<Setter Property="RowDefinition.Height" Value="2*"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ShowSection}" Value="False">
<Setter Property="RowDefinition.Height" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RowDefinition.Style>
</RowDefinition>
这编译,没有抛出错误,但似乎没有任何效果.是我遗漏了什么,还是 Grid 不允许在绘制表单后调整其行的大小或类似的东西?
This compiles, throws no errors, but doesn't seem to have any effect. Is there something I'm missing, or does the Grid not allow its rows to resize after the form is drawn or something to that effect?
推荐答案
我认为您的 Xaml 代码的唯一问题是您通过在 RowDefinition
上显式设置 Height 来覆盖 DataTrigger.尝试使用 Setter 代替
I think the only problem with your Xaml code is that you're overwriting the DataTrigger by setting Height explictly on the RowDefinition
. Try with using a Setter instead
<RowDefinition>
<RowDefinition.Style>
<Style>
<Setter Property="RowDefinition.Height" Value="2*"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=ShowSection}" Value="True">
<Setter Property="RowDefinition.Height" Value="2*"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=ShowSection}" Value="False">
<Setter Property="RowDefinition.Height" Value="0"/>
</DataTrigger>
</Style.Triggers>
</Style>
</RowDefinition.Style>
</RowDefinition>
这篇关于是否可以在 Grid RowDefinitions 上使用触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!