WPF 中的双向数据绑定到底是什么?
我正在使用 MVVM 学习 WPF,作为初学者,我观看了 Jason Dolinger 关于 MVVM 的视频。他在其中提到,将 MVVM 与 WPF 结合使用的优点之一是双向数据绑定。我的问题是双向数据绑定是什么意思?该功能是否能够 1)将数据从控件(视图)绑定到 VM 中的属性,以及 2)集合或属性中的任何更改都反映在视图中?如果我错了,谁能详细解释一下它是什么以及它的优点是什么?这可能是一个非常简单的疑问,但我对 WPF 及其术语非常陌生,并且现在正在远离 WinForms。
I'm learning WPF with MVVM and for a starter, I viewed Jason Dolinger's video on MVVM . In that he mentioned, that one of the advantage of using MVVM with WPF is two-way data binding. My question is what does he mean by two-way data binding? Is that the feature of ability to 1) bind data from controls(View) to properties in VM and 2) any change in collections or properties are reflected in the view? If I'm wrong, can anyone please explain me in detail what it is and what are its advantages? This might be a very simple doubt, but I'm very new to WPF and it's terminologies and moving away from WinForms now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你没有错。双向数据绑定正是这个含义。
您将视图与其 ViewModel 连接起来,因此您在其中一个视图中所做的每一项更改都会反映到另一个视图中。
要获得这种行为,您必须在 ViewModel 中实现接口 INotifyPropertyChange 或使用依赖属性,而不是普通的 CLR 属性。
您还可以使用其他类型的数据绑定,例如一种方式或一种来源方式。然而默认是双向的。
希望这有帮助。问候
You're not wrong. Two way data binding has exactly that meaning.
You have the view connected with its ViewModel, so every change you make in one of them is reflected into the other.
To obtain that kind of behavior, you have to implement the interface INotifyPropertyChange in your ViewModel or using Dependency Properties, instead of normal CLR properties.
You can also use other types of data binding, like one way or one way to source. The default however is two-way.
Hope this helps. Regards
总体思路是,不仅对 DataContext 的任何更改都会反映在 UI 中,而且对 UI 的更改也会被推回到 DataContext 中。
所以你是对的:)
The general idea is that not only is any change to the DataContext reflected in the UI but also changes to the UI are pushed back into the DataContext.
So you are right :)