是否可以使用 setter 将属性返回到其默认值? [英] Is it possible to use a setter to return a property to its default value?

查看:67
本文介绍了是否可以使用 setter 将属性返回到其默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道这是否可能,例如如果我从 TextBox 中删除边框,并且希望在鼠标悬停在其上方时恢复其默认边框.

Wondering if that is possible, e.g. if i remove the border from a TextBox and i want to have its default border back when the mouse is over it.

    <Style TargetType="TextBox">
        <Setter Property="BorderBrush" Value="{x:Null}"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="BorderBrush" Value="?????"/>
            </Trigger>
        </Style.Triggers>
    </Style>

我以为我可以使用它,但最终隐藏边框似乎是一个坏主意,但问题仍然存在.(我知道在这种情况下,如果鼠标不在 TextBox 上方,我可以反转触发器以仅删除边框)

I thought i could use that but in the end it seemed like a bad idea to hide the border, but the question remains. (I know that in this case i could reverse the Trigger to only remove the border if the mouse is not over the TextBox)

推荐答案

它实际上并不是那样工作的.DependencyProperty 系统通过考虑许多不同的值源来工作.正如您在此处所见,样式设置器和样式触发器是被认为是单独的来源.(它们在依赖属性设置优先级列表"部分中分别编号为 8 和 6.)具有最高优先级的活动值源获胜.

It doesn't really work like that. The DependencyProperty system works by considering numerous different value sources. And as you can see here, style setters and style triggers are considered to be separate sources. (They're numbered 8 and 6 respectively in the "Dependency Property Setting Precedence List" section.) Whichever of the active value sources has the highest priority wins.

一种属性源不能删除不同源提供的值.触发器能够改变 setter 设置的值的唯一原因是触发器具有更高的优先级.没有权力根除低优先级来源提供的价值.

Property sources of one kind cannot remove the value provided by a different source. The only reason triggers are able to change the value from what a setter sets it to is that triggers have a higher priority. There's no power to eradicate a value supplied by a lower-priority source.

实现您在此处表达的特定目标的方法是反转触发器的含义 - 不要定义样式设置器,并且仅在 IsMouseOver 为 false 时才激活触发器.当然,在您可能想要执行此操作的所有可能情况下,这不会对您有所帮助.但由于没有通用的解决方案,我认为您需要以自己的方式解决每个具体问题.

The way to achieve the particular goal you've expressed here would be to invert the sense of the trigger - don't define a style setter, and make the trigger active only when IsMouseOver is false. Of course, that won't help you in all the possible cases where you might want to do this. But since there isn't a general solution, I think you'd need to solve each specific problem in its own way.

这篇关于是否可以使用 setter 将属性返回到其默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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