内容更改之前的 WPF DependencyProperty 事件
首先我将解释问题的背景,因为你也许可以为我指明更好的方向。
我需要在对象上实现类似撤消重做的系统。该对象具有一系列依赖属性。有些是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的情况下,属性更改回调是构造函数的第二个参数,而不是您使用的第三个参数,即值强制回调。
编辑:为了回应您的评论,请仔细检查 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.