是否可以在网格行定义上使用触发器?

发布于 2024-10-19 04:36:09 字数 782 浏览 1 评论 0原文

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

<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>

这可以编译,不会引发任何错误,但似乎没有任何效果。是否有我遗漏的东西,或者网格是否不允许在绘制表单后调整其行大小或达到此效果?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兲鉂ぱ嘚淚 2024-10-26 04:36:09

我认为您的 Xaml 代码的唯一问题是您通过在 RowDefinition 上显式设置高度来覆盖 DataTrigger。尝试使用 Setter 代替

<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>

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>
我ぃ本無心為│何有愛 2024-10-26 04:36:09

我知道这篇文章已经有 3 年历史了,但它仍然可能对某人有所帮助。

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

我处于同样的情况,我设法通过将 TargetType 设置为 RowDefinition 来使其工作。

I know this post is 3 years old, but still it might help someone.

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

I was in same situation, i managed to get it working by setting TargetType to RowDefinition.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文