是否可以在Grid RowDefinitions上使用触发器? [英] Is it possible to use Triggers on Grid RowDefinitions?

查看:204
本文介绍了是否可以在Grid RowDefinitions上使用触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格,其行需要根据视图模型动态调整大小。我想要做类似下面的事情:

 < RowDefinition Height =2 *> 
< RowDefinition.Style>
< Style>
< Style.Triggers>
< DataTrigger Binding ={Binding Path = ShowSection}Value =True>
< Setter Property =RowDefinition.HeightValue =2 */>
< / DataTrigger>
< DataTrigger Binding ={Binding Path = ShowSection}Value =False>
< Setter Property =RowDefinition.HeightValue =0/>
< / DataTrigger>
< / style>
< /RowDefinition.Style>
< / RowDefinition>

这个编译,不会引发任何错误,但似乎没有任何影响。是否有我遗失的东西,或者网格不允许它的行在表单被绘制之后调整大小或出现这种效果?

我认为你的Xaml代码唯一的问题是你通过在 RowDefinition 中显式设置Height来覆盖DataTrigger。尝试使用Setter而不是

 < RowDefinition> 
< RowDefinition.Style>
< Style>
< Setter Property =RowDefinition.HeightValue =2 */>
< Style.Triggers>
< DataTrigger Binding ={Binding Path = ShowSection}Value =True>
< Setter Property =RowDefinition.HeightValue =2 */>
< / DataTrigger>
< DataTrigger Binding ={Binding Path = ShowSection}Value =False>
< Setter Property =RowDefinition.HeightValue =0/>
< / DataTrigger>
< / style>
< /RowDefinition.Style>
< / RowDefinition>


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>

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?

解决方案

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上使用触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆