内容更改之前的 WPF DependencyProperty 事件

发布于 2024-09-02 03:53:01 字数 855 浏览 8 评论 0原文

首先我将解释问题的背景,因为你也许可以为我指明更好的方向。

我需要在对象上实现类似撤消重做的系统。该对象具有一系列依赖属性。有些是 double、int、string,但有些也是 DependencyObject 类型。 我需要在更改属性之前保存属性的值,为此我添加了 CoerceValueCallback。

public static readonly DependencyProperty MyBackgroundProperty =
        DependencyProperty.Register("MyBackground", typeof(MyCustomizableBackground),
            typeof(MyComponent), new UIPropertyMetadata(default(MyCustomizableBackground), null, new CoerceValueCallback(OnPropertyChanging)));

在 OnPropertyChanging 中,我在更改之前保存该值。 MyCustomizedBackground 是 DependencyObject,它也具有一些依赖属性。

问题是,在这种情况下,当我有一个自定义对象作为属性时,不会触发 OnPropertyChanging 方法,但当我有一个通用类型时,它会被触发。

后来编辑:我意识到我的问题的一部分相当模糊,我问了一个单独的问题这里< /a>.对于问题的第一部分,Julien 为我指明了更好的方向。

First I will explain the context of the problem, because you might be able to point me in a better direction.

I need to implement a undo-redo like system on an object. The object has a series of dependency properties. Some are double, int, string but some are also of DependencyObject type.
I need to save the value of the property before it is changed, and for this I added the CoerceValueCallback.

public static readonly DependencyProperty MyBackgroundProperty =
        DependencyProperty.Register("MyBackground", typeof(MyCustomizableBackground),
            typeof(MyComponent), new UIPropertyMetadata(default(MyCustomizableBackground), null, new CoerceValueCallback(OnPropertyChanging)));

In OnPropertyChanging I save the value before it's changed. MyCustomizableBackground is the DependencyObject that has also some dependency properties.

The problem is that in this case, where I have a custom object as a property, the OnPropertyChanging method isn't triggered, but when I have a common type, it is triggered.

Later edit: I realised that a part of my question was quite ambiguous and I asked a separate question here. For the first part of the problem, Julien pointed me in a better direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2024-09-09 03:53:01

在您的情况下,属性更改回调是构造函数的第二个参数,而不是您使用的第三个参数,即值强制回调。

编辑:为了回应您的评论,请仔细检查 MyComponent 是否是拥有该属性的良好类型。我记得不久前在复制/粘贴 DP 并忘记更改所属类型后遇到了类似的问题。

The property changed callback is the second parameter of the constructor in your case, not the third as you used which is the value coercion callback.

Edit: in response to your comment, double check that MyComponent is the good type owning the property. I remember having a similar problem a while ago after copy/pasting a DP and forgetting to change the owning type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文