在WPF中,对于DP,是否可以使用函数返回默认值或在吸气剂一侧具有CoerceValue? [英] In WPF, for a DP, is there any way to use a function to return the default value or to have a CoerceValue on the getter side?

查看:70
本文介绍了在WPF中,对于DP,是否可以使用函数返回默认值或在吸气剂一侧具有CoerceValue?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中有一种特殊情况,我需要通过XAML设置一个依赖属性,该属性实际上并不会存储,而是用于设置其他依赖属性。这很容易,因为我只是使用CoerceValue来拦截更改,使用传入的值执行我想要的操作,然后取消该属性上的更改。这意味着当我获得该属性后,由于阻止了实际更改,因此我仍将获得默认值。效果很好。

There's an exceptional case in WPF where I need to set a dependency property via XAML that actually doesn't get stored itself, but rather is used to set other dependency properties. This is pretty easy since I just use the CoerceValue to intercept the change, do what I want with the passed-in value, then cancel the change on that property. This means when I get that property, I'll still get the default value since I blocked the actual change. Works great.

例如,当我设置此人造CanvasRect = 10,10,40,20 DP时,实际上在内部设置了Canvas.Left,Canvas .Top,Width和Height DP和CanvasRect本身仍保持默认值 0,0,0,0。

As an example, when I set this faux CanvasRect="10,10,40,20" DP, internally it actually sets the Canvas.Left, Canvas.Top, Width and Height DPs and CanvasRect itself still remains at its default of "0,0,0,0".

但是,我有一个想法,如果我而是要读取该属性,我想根据其他依赖项属性对该值进行重新水化处理。这可以通过简单的单行函数来完成...

However, I had an idea that if I were to instead read the property, I'd want to re-hydrate that value based on those other dependency properties. This can be done with a simple one-line function...

return new rect(Canvas.GetLeft(this), Canvas.GetTop(this), Width, Height);

将其视为一种CoerceValue,但在getter方面,而不是setter上。但是,我不确定如何或什至可以使用函数强制像这样的回读值。

Think of it as sort of a CoerceValue but on the getter side, not the setter. However, I'm not sure how, or even if you can use functions to coerce the read-back values like that.


注意:是的,我知道此方法会使CanvasRect DP的任何更改通知
无效,但是我不在乎,也不是我真正想要的。如果我做了
,我会存储一个实际的
rect,然后通过订阅属性$ can $ Canb.Left,Canvas.Top的
事件来保持同步。 ,
宽度和高度DP,
在功能上会给我
想要的东西,但这比实际的DP更像
的便利。
另外,这是一个非常简单的示例,可以显示我的要求。
现实世界中的示例可能是基于时间的
,例如,无论如何DP的
更改通知都无济于事。

Note: Yes, I know this method would invalidate any change notifications for the CanvasRect DP but I don't care, nor do I actually want that. If I did, I'd instead store an actual rect, then keep it in sync by subscribing to the property changed events of the Canvas.Left, Canvas.Top, Width and Height DPs, which functionally would give me what I want, but this is more of a convenience thing than an actual DP. Plus this is a very simple example to show what I'm asking. The real-world example may be time-based for instance where no DP change notifications would matter anyway.

现在我知道我也可以简单地在.get包装属性的getter中实现我想要的东西,但是通过绑定等读取时,.NET属性包装将被绕过这就是为什么MSDN表示除了方便地委派DP之外,不要将其用于任何其他用途。另外,实际上,对于OneTime以外的任何内容,这再没有意义了,不会发生任何更改通知。

Now I know I could also simply implement what I wanted in the getter for the .NET wrapper property, but when reading via bindings and such, the .NET property wrapper gets bypassed which is why MSDN says to never use it for anything except for convenient delegation tp the DP. Plus, realistically, for anything other than OneTime, this wouldn't make sense anyway as again, no change notifications would occur.

无论如何,DP设置器效果很好,但是如果我确实想放入吸气剂,除了将所有这些更改通知关联到相关DP之外,我不确定是否可以采用其他方法。

Anyway, as I said, the DP setter works great, but if I did want to put in the getter, I'm not sure I can think of any other way except to wire up all those change notifications for the related DPs.

为什么要执行此操作的另一个示例,是因为您想要在属性getter自身中使用返回值转换器作为返回值,也许将返回值保持在一定范围内,但又不丢失实际存储的值值用于其他内部用途。

Another example of why you may want to do this is because you want to use a value converter for the return value within the property getter itself, perhaps to keep the return values within a certain range, but not lose the actual stored values for other internal purposes.

但是回到这里的基本问题:是否有任何方法可以将函数用作DP的默认值,或者至少使用CoerceValue

But back to the basic question here: is there any way to use a function as the default value of a DP, or at least a CoerceValue on the getter side?

推荐答案

值强制仅适用于更改DP的有效值。它不适用于读取DP的值。您可以创建另一个分配了CanvasRect值的DP,例如CachedCanvasRect,并在CanvasRect更改值时更新缓存的DP。

Value coercion only applies to changing the effective value of a DP. It does not apply to reading a DP's value. You could possibly create another DP that is assigned the value of CanvasRect, such as CachedCanvasRect, and update the cached DP when CanvasRect changes value.

这篇关于在WPF中,对于DP,是否可以使用函数返回默认值或在吸气剂一侧具有CoerceValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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