WPF:绑定到对象的属性;当对象改变时会发生什么? [英] WPF: bind to an object's property; what happens when the object changes?

查看:339
本文介绍了WPF:绑定到对象的属性;当对象改变时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您有一个对象,该对象具有属性和绑定到该对象属性的控件。更改该对象时会发生什么?

Let's say you have an object with a property and a control that is binding to a property of that object. What happens when that object is changed?

例如,ViewModel:

For example, ViewModel:

public class TheViewModel : INotifyPropertyChanged
{
        private TheObjectClass theObject;
        public TheObjectClass TheObject
        {
            get { return theObject; }
            set { theObject = value; 
                  OnPropertyChanged("TheObject"); }
        }
}

对象的类:

public class TheObjectClass : INotifyPropertyChanged
{
        private ThePropertyClass theProperty;
        public ThePropertyClass TheProperty
        {
            get { return theProperty; }
            set { theProperty= value; 
                  OnPropertyChanged("TheProperty"); }
        }
}

物业等级:

public class ThePropertyClass : INotifyPropertyChanged
{
        private object objectToBindTo;
        public object ObjectToBindTo
        {
            get { return objectToBindTo; }
            set { objectToBindTo= value; 
                  OnPropertyChanged("ObjectToBindTo"); }
        }
}

然后在一个窗口中(具有ViewModel设置为DataContext),就可以绑定到该对象,例如:

And then in a window (that has the ViewModel set as it's DataContext) you have a control that binds to this object, like:

<TextBlock Text={Binding TheObject.TheProperty.ObjectToBindTo}/>

然后将ViewModel.TheObject重置在某个位置(绑定完成后):

Then you reset ViewModel.TheObject somewhere (after the binding has been made):

ViewModel.TheObject = new TheObjectClass();

在我的测试中(并非专门针对此代码,这只是我代码中的简化示例应用程序)似乎有时绑定仍然存在,有时它停止工作。我应该在这里发生什么?在我看来,当父对象实例化为另一个对象时,属性绑定会中断,但是有时候它似乎仍然存在,这确实使我感到困惑。

In my testing (not specifically of this code, which is just a simplified example of the code in my application) it seems that sometimes the Binding survives, and sometimes it stops working. What should I expect to happen here? It seems logical to me that the property binding would break when the parent object is instantiated to a different object, but sometimes it seems to survive which is really confusing me.

推荐答案

通常,这是受支持的操作,并且绑定应始终存活。 WPF绑定系统会在 TheObject 上看到属性更改通知,并应将绑定重置为新的子对象。

In general, this is a supported operation, and the binding should always "survive". The WPF binding system sees the property changed notification on TheObject, and should reset the binding to the new subobjects.

在实践中,绑定有时会无法生存,但这通常是因为在某个时间点上,沿着链的一个对象变为 null ,这会中断绑定。如果一个对象变为 null ,则WPF有效地取消订阅 PropertyChanged 调用(因为没有要监听的对象),并且永远不会看到设置了新值,这又会阻止它起作用。

In practice, the binding will occasionally not survive, but this is normally because there is a point in time where one object along the chain becomes null, which then breaks the binding. If one object becomes null, WPF effectively unsubscribes from the PropertyChanged call (as there's no object to listen on), and never sees the new value get set, which in turn can prevent it from functioning.

这篇关于WPF:绑定到对象的属性;当对象改变时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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