绑定如何实际工作?

时间:2022-08-25 17:26:38

I've been learning WPF for a few months now and I'm curious about one thing. How does a Binding actually work? I mean, what happends, under the hood. I don't expect that anyone here would give a detailed explanation but maybe a good resource or link where to read up on something like this. I've been searching and googling for this but no good hits so far.

我已经学习了几个月的WPF,我很好奇一件事。绑定如何实际工作?我的意思是,在幕后发生了什么。我不希望这里的任何人会给出详细的解释,但可能是一个很好的资源或链接,在那里阅读这样的东西。我一直在搜索和谷歌搜索,但到目前为止还没有很好的点击。

I realize that to fully understand this you would probably have to understand most parts of the framework but a little basic understanding would be great.

我意识到要完全理解这一点,你可能必须理解框架的大部分内容,但是一点基本的理解会很棒。

Thanks

谢谢

3 个解决方案

#1


2  

There are two aspects to consider in binding, getting values in to the UI and having the UI be notified of changes in its DataContext.

在绑定中需要考虑两个方面,将值添加到UI并向UI通知其DataContext中的更改。

Basically you can bind almost anything to any POCO object, the object does not need to implement anything special. The restriction with plain objects is the binding target will not be told when the underlying value changes.

基本上你几乎可以将任何东西绑定到任何POCO对象,该对象不需要实现任何特殊的东西。使用普通对象的限制是在基础值更改时不会告知绑定目标。

Property changes are propogated through one of three mechanisims:

财产变化通过以下三种机制之一传播:

Dependancy Properties : will notify the binding system when its value changes, can also be used for animations.

依赖属性:当其值发生变化时将通知绑定系统,也可用于动画。

INotifyPropertyChanged : If the binding is to a property on an object which implements INotifyPropertyChanged, the binding system will look for subscribe to the PropertyChanged event and update the binding target, when this event is raised, property names are sent as Strings.

INotifyPropertyChanged:如果绑定是实现INotifyPropertyChanged的对象的属性,绑定系统将查找订阅PropertyChanged事件并更新绑定目标,当引发此事件时,属性名称将作为字符串发送。

*Property*Changed events : The last thing bindings will look for is an event with a name the same as the source property and Changed on the end, so a Name property would need to have a public event called NameChanged, this allows WPF to bind to older .net classes such as 1.1.

*属性*更改事件:绑定最后要查找的是一个名称与source属性相同且在末尾更改的事件,因此Name属性需要有一个名为NameChanged的公共事件,这允许WPF绑定到较旧的.net类,如1.1。

#2


3  

I don't know much about the specifics of binding in WPF, but if it's the same principle behind binding in System.ComponentModel and Windows Forms, then a blog article I wrote recently might help to shed some light on it for you:

我对WPF中的绑定细节知之甚少,但如果它与System.ComponentModel和Windows Forms中的绑定背后的原理相同,那么我最近写的一篇博客文章可能有助于为您阐明:

Some Background on Windows Forms Data Binding

Windows窗体数据绑定的一些背景知识

Basically, binding sources have to implement IList, IListSource, ITypedList or IBindingList. Through either reflection or explicit definition, data sources expose PropertyDescriptor objects which describe their bindable properties. The names of these properties (which may or may not be the names of actual properties on the objects contained in the data source - e.g. in DataTable, they are column names instead) are matched against the DisplayMember/ValueMember property or, in the case of WPF, the binding path.

基本上,绑定源必须实现IList,IListSource,ITypedList或IBindingList。通过反射或显式定义,数据源公开描述其可绑定属性的PropertyDescriptor对象。这些属性的名称(可能是也可能不是数据源中包含的对象上的实际属性的名称 - 例如,在DataTable中,它们是列名称)与DisplayMember / ValueMember属性匹配,或者在WPF,绑定路径。

#3


0  

This is quite a difficult question to answer. I believe there are roughly two aspects to an answer. The first is the documentation. If you go through all the documentation of binding expressions, including how paths are build and e.g. the helper classes like BindingOperations, you can find out a lot. If that's not enough, you can always dive into the code by downloading it from the Microsoft Source Initiative site.

这是一个非常难以回答的问题。我相信答案大致有两个方面。第一个是文档。如果您浏览了绑定表达式的所有文档,包括如何构建路径,例如像BindingOperations这样的辅助类,你可以找到很多。如果这还不够,您可以通过从Microsoft Source Initiative站点下载代码来深入了解代码。

#1


2  

There are two aspects to consider in binding, getting values in to the UI and having the UI be notified of changes in its DataContext.

在绑定中需要考虑两个方面,将值添加到UI并向UI通知其DataContext中的更改。

Basically you can bind almost anything to any POCO object, the object does not need to implement anything special. The restriction with plain objects is the binding target will not be told when the underlying value changes.

基本上你几乎可以将任何东西绑定到任何POCO对象,该对象不需要实现任何特殊的东西。使用普通对象的限制是在基础值更改时不会告知绑定目标。

Property changes are propogated through one of three mechanisims:

财产变化通过以下三种机制之一传播:

Dependancy Properties : will notify the binding system when its value changes, can also be used for animations.

依赖属性:当其值发生变化时将通知绑定系统,也可用于动画。

INotifyPropertyChanged : If the binding is to a property on an object which implements INotifyPropertyChanged, the binding system will look for subscribe to the PropertyChanged event and update the binding target, when this event is raised, property names are sent as Strings.

INotifyPropertyChanged:如果绑定是实现INotifyPropertyChanged的对象的属性,绑定系统将查找订阅PropertyChanged事件并更新绑定目标,当引发此事件时,属性名称将作为字符串发送。

*Property*Changed events : The last thing bindings will look for is an event with a name the same as the source property and Changed on the end, so a Name property would need to have a public event called NameChanged, this allows WPF to bind to older .net classes such as 1.1.

*属性*更改事件:绑定最后要查找的是一个名称与source属性相同且在末尾更改的事件,因此Name属性需要有一个名为NameChanged的公共事件,这允许WPF绑定到较旧的.net类,如1.1。

#2


3  

I don't know much about the specifics of binding in WPF, but if it's the same principle behind binding in System.ComponentModel and Windows Forms, then a blog article I wrote recently might help to shed some light on it for you:

我对WPF中的绑定细节知之甚少,但如果它与System.ComponentModel和Windows Forms中的绑定背后的原理相同,那么我最近写的一篇博客文章可能有助于为您阐明:

Some Background on Windows Forms Data Binding

Windows窗体数据绑定的一些背景知识

Basically, binding sources have to implement IList, IListSource, ITypedList or IBindingList. Through either reflection or explicit definition, data sources expose PropertyDescriptor objects which describe their bindable properties. The names of these properties (which may or may not be the names of actual properties on the objects contained in the data source - e.g. in DataTable, they are column names instead) are matched against the DisplayMember/ValueMember property or, in the case of WPF, the binding path.

基本上,绑定源必须实现IList,IListSource,ITypedList或IBindingList。通过反射或显式定义,数据源公开描述其可绑定属性的PropertyDescriptor对象。这些属性的名称(可能是也可能不是数据源中包含的对象上的实际属性的名称 - 例如,在DataTable中,它们是列名称)与DisplayMember / ValueMember属性匹配,或者在WPF,绑定路径。

#3


0  

This is quite a difficult question to answer. I believe there are roughly two aspects to an answer. The first is the documentation. If you go through all the documentation of binding expressions, including how paths are build and e.g. the helper classes like BindingOperations, you can find out a lot. If that's not enough, you can always dive into the code by downloading it from the Microsoft Source Initiative site.

这是一个非常难以回答的问题。我相信答案大致有两个方面。第一个是文档。如果您浏览了绑定表达式的所有文档,包括如何构建路径,例如像BindingOperations这样的辅助类,你可以找到很多。如果这还不够,您可以通过从Microsoft Source Initiative站点下载代码来深入了解代码。