如果在元素中设置,WPF触发器将不会设置属性 [英] WPF Trigger won't set property if set in Element

查看:121
本文介绍了如果在元素中设置,WPF触发器将不会设置属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这种行为对我来说似乎非常奇怪,我想我做错了什么。我有一个 ContentControl ,它使用DataTemplete呈现TabControl。我希望在没有选项卡打开时显示图像,而在没有选项卡时隐藏。但这是问题所在:

This behavior seems incredibly odd to me, and I assume I am doing something wrong to get it. I have a ContentControl that uses a DataTemplete to render an TabControl. I want an image to display when there are no tabs open, and hide when there are. But here is the problem:

<Image Name="image1" Stretch="Uniform" Visibility="Hidden" Source="/Affinity;component/Images/affinity_logo.png">
            <Image.Style>
                <Style TargetType="Image">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Items.Count, ElementName=tabcontrolworkspaces}"
                        Value="0">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Image.Style>
        </Image>

这不起作用...有点。

This doesn't work... sort of.

我已经在可见性和保证金上对此进行了测试(请确保)。除非在 Image 标记中定义了该属性,否则此触发器将更改该属性。如果是这样,触发器将不会更新该属性。因此,如果我没有为图像定义可见性,并且触发器将其隐藏,则可以正常工作。问题是,默认值为可见,触发器需要在value = 0时显示它,否则将其隐藏。

I have tested this on Visiblity and Margin (just to be sure). This trigger will alter the property, unless that property is defined in the Image tags. If it is, the trigger will not update that property. So, if I don't define a visibility for the image, and the trigger hides it, it works. The problem is, the default is Visible and the trigger needs to show it when value=0 and hide it otherwise.

为什么触发器不会覆盖显式的属性定义的?

Why won't the trigger override properties that are explicitly defined? Isn't that its purpose?

推荐答案

这是正常的相关性属性值优先级。在图像上将其设置为#3,在样式触发器中将其设置为较低优先级#6。

This is the normal Dependency Property Value Precedence. Setting it on Image is at #3, while in the Style trigger is at a lower precedence of #6.

您可以改为:

<Image Name="image1" Stretch="Uniform" Source="/Affinity;component/Images/affinity_logo.png">
    <Image.Style>
        <Style TargetType="Image">
            <Setter Property="Visibility" Value="Hidden" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Items.Count, ElementName=tabcontrolworkspaces}"
                    Value="0">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>

这篇关于如果在元素中设置,WPF触发器将不会设置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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