分层 INotifyPropertyChanged
我有一个为属性实现 INotifyPropertyChanged 的类。
我有一个绑定到该属性的控件。
我有另一个类监听 propertychanged 事件。 在该类的事件处理程序中,我更改了代码中属性的值。
我遇到的问题是,我不想在事件处理程序中执行任何逻辑,以便下次由于代码导致属性更改而触发事件处理程序。
但是,如果用户同时更改属性的值(通过异步 gui 输入),我仍然希望触发逻辑。 我还需要确保控件得到更新(这是双向绑定)。
在不让事情变得一团糟的情况下,最好的方法是什么?
I have a class that implements INotifyPropertyChanged for a property.
I have a control that is bound to that property.
I have another class that listens to the propertychanged event. In the event handler of that class I change the value of the property in code.
The problem I have is that I don't want to do any logic in the event handler for the next time it will fire due to the change of the property due to code.
However if the user changes the value of the property in the mean time (via async gui input) I still want the logic to fire. I also need to make sure that the control gets updated (this is twoway binding).
What is the best way to do this without this becoming a complete mess?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现此目的的一种方法是重构属性上的 setter,以便它调用一个方法,该方法采用一个指示是否引发事件的参数。 这是一个简单的代码示例:
One way to accomplish this would be to refactor the setter on your property so that it called a method taking a parameter indicating whether or not to raise the event. Here is a simple code sample:
您的问题有点模糊,但您可以检查该值是否确实发生了变化,以确定是否应该执行您的逻辑。
如果您包含一些代码以更具体地表达您的问题,那就更好了。
Your question is a bit vague, but you can check to see if the value has actually changed to determine if you should do your logic.
It would be better if you include some code to be more specific in your question.