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

查看:31
本文介绍了如果在 Element 中设置,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.

我已经在 Visiblity 和 Margin 上测试过这个(为了确定).此触发器将更改该属性,除非该属性已在 Image 标记中定义.如果是,触发器将不会更新该属性.所以,如果我没有定义图像的可见性,并且触发器隐藏它,它就可以工作.问题是,默认是 Visible,触发器需要在 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?

推荐答案

这是正常的依赖属性值优先级.将它设置在 Image 上是 #3,而在 Style 触发器中的优先级较低,为 #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>

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

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