手动调用 WPF Setter [英] Invoke WPF Setter manually

查看:46
本文介绍了手动调用 WPF Setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调用在后面的代码中传递的 Setter(特定于 WPF).

I would like to call a Setter (WPF specific) passed in a code behind.

void Invoke(Setter setter)
{
   //I am interested what to do here, ?
}

setter 类似于

<Setter TargetName="SomeUiElement" Property="SomeProperty" Value="{Binding SomeValue}" />

//resolution similar to
void Call(Setter setter)
{
   setter.Property.SetValue(setter.TargetObject, setter.Value.GetValue())
}

提前致谢.请具体.

推荐答案

Setter 并不像它们看起来的那样.当 WPF 激活它们时,它们不会严格设置属性的值(尽管名称不同).相反,它们贡献了一个可能的价值,这可能会或可能不会影响财产的有效价值.(这取决于更高优先级的属性值源当前是否处于活动状态.http://msdn.microsoft.com/en-us/library/ms743230.aspx 记录了 11 个不同的属性值可能来自的地方.)当 setter 停用时,它们不再提供该值.

Setters are not quite what they seem. When WPF activates them, they don't strictly set a property's value (despite the name). Instead, they contribute a possible value, which may or may not have an effect on the property's effective value. (It depends on whether a higher priority property value source is currently active. http://msdn.microsoft.com/en-us/library/ms743230.aspx documents 11 different places property values can come from.) And when setters deactivate, they no longer offer that value.

我提到这一点的原因是为了解释为什么从来没有真正调用"setter.调用是即时的 - 它是在特定时刻发生的事情.另一方面,二传手在一段时间内是活跃的.(所以有两个有趣的时刻:它们变得活跃的时刻,以及它们停止活跃的时刻.)

The reason I mention this is to explain why a setter is never really "invoked". Invocation is instantaneous - it's a thing that happens at a particular moment. Setters, on the other hand, are active for some duration. (So there are two interesting instants: the point at which they become active, and the point at which they cease to be active.)

因此,如果您要问如何调用 setter,那么您可能没有适合您尝试做的任何工作的正确工具.可能发生的最接近的情况是 setter 变为活动状态,并在您希望的时间内保持活动状态.

So if you're asking how to invoke a setter, you may not have the right tool for whatever job you're trying to do. The closest that can happen is that the setter becomes active, and remains active for however long you want it to.

此外,setter 的激活并不能保证它的目标属性实际上会改变,因为可能还有其他更高优先级的值来源.此外,为了清楚甚至激活应该意味着什么,您需要知道设置器的使用方式 - 默认样式中的设置器的优先级低于应用程序定义的样式中的设置器,反过来比模板中的触发器激活的 setter 具有更低的优先级...

Moreover, activation of a setter doesn't guarantee that its target property will actually change, because there may be other, higher priority value sources. Moreover, for it to be clear even what activation should mean, you need to know the way in which the setter is being used - the setters in the default style have a lower predence than those in a style defined by the application, which in turn have a lower priority than setters activated by a trigger in a template...

因此,孤立的 setter 是没有意义的,因为它的行为(即,当它变得活跃时会做什么)是由 setter 出现的上下文定义的.

So a setter in isolation is meaningless, because its behaviour (i.e., what it does when it becomes active) is defined by the context in which the setter appears.

现在从您的代码中,看起来您真正想要做的是将 setter 目标的本地值设置为属性将获得的任何值,如果 a) setter 变为活动状态并且 b) setter 是该属性可用的最高优先级值.但这并不是 setter 真正知道如何做的事情.

Now from your code, it looks like what you really want to do is arrange for the Local Value of the setter's target to be set to whatever value the property would acquire if a) the setter became active and b) that setter was the highest priority value available for that property. But that's not something a setter actually knows how to do.

是的,这很讽刺,但 setter 实际上并不知道如何设置属性.

Yes, it's ironic, but setters don't actually know how to set properties.

使 setter 的值成为目标属性值的代码实际上分布在 WPF 的各个部分(包括 TriggerStyle 类等).没有一种直接的方法可以让 WPF 做你想做的事情,因为它没有办法孤立地做——WPF 自己的 setter 处理与属性系统的许多其他方面混合在一起.我强烈建议您尝试寻找其他方法来解决您希望以这种方式解决的任何问题.

The code that causes the setter's value to become the target property value is actually distributed across various parts of WPF (including the Trigger and Style classes, amongst others). There isn't a straightforward way of getting WPF to do what you want, because it doesn't have a way of doing it in isolation - WPF's own processing of setters is mixed in with many other aspects of the property system. I'd strongly recommend trying to find some other way to solve whatever problem you were hoping to solve this way.

这篇关于手动调用 WPF Setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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