绑定到自定义类 [英] Binding to a custom class

查看:74
本文介绍了绑定到自定义类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绑定到我创建的自定义类(即WpfFlags 类),但是无法获取随更新而更新的值,并希望有人可以帮助我.我知道绑定正在工作,因为在加载视图时正在执行PropertyChangedCallback ,但是执行PropertyChanged(this, new PropertyChangedEventArgs("GraphicFlags")) 不会导致属性被更新.我知道代码在工作,因为我做了一个变通办法,即我绑定到类(int32)中的关键属性,然后为此DependencyProperty 中的PropertyChangedCallback 重新创建WpfFlags 类的实例.查看后面的代码.然后,在后面的代码中更改WpfFlags DependencyProperty 的值,然后创建所需的结果.

我试图从WpfFlags 类的DependencyObject 继承,但这没有任何效果.我也研究了TypeDescriptor,但我认为这不是答案.

帮助将不胜感激.

I have attempted to bind to a custom class I have created (i.e., WpfFlags class), but have been unable to get the value to update with change, and was hoping somebody could help me. I know the binding is working because the PropertyChangedCallback is being executed when the view loads, but executing PropertyChanged(this, new PropertyChangedEventArgs("GraphicFlags")) is not causing the property to be updated. I know that the code is working because I did a workaround where I bind to the critical proptery in the class (int32), and then recreate an instance of the WpfFlags class in the PropertyChangedCallback for this DependencyProperty in View’s code behind . Then I change the value of the WpfFlags DependencyProperty in the code behind, which then does create the desired result.

I attempted to inherit from a DependencyObject for the WpfFlags class, but that had no effect. I also have investigated the TypeDescriptor, but I do not think that is the answer.

Help would be very much appreciated.

推荐答案

这是一个问题,因为绑定系统无法理解对象(类实例)已更改.解决此问题的方法是将属性更改为该类的全新实例,然后引发属性更改事件.不幸的是,据我所知,没有办法告诉Binding类中的值已更改,因此需要重新计算绑定值.更改GetHashCode返回的值也无济于事.
This was a problem with the fact that the binding system did not understand that the object (class instance) changed. The way to fix this is to change the property to a totally new instance of the class and then raise the property changed event. unfortunately as far as I can tell there is no way to tell the Binding that a value within the class has changed and thus needs to recalculated binding values. Changing the value returned by GetHashCode did not help either.


这篇关于绑定到自定义类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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