简体   繁体   English

WPF中的双向数据绑定到底是什么?

[英]What exactly is Two-way databinding in WPF?

I'm learning WPF with MVVM and for a starter, I viewed Jason Dolinger's video on MVVM . 我正在通过MVVM学习WPF,对于初学者来说,我观看了Jason Dolinger在MVVM上的视频。 In that he mentioned, that one of the advantage of using MVVM with WPF is two-way data binding. 在他提到的内容中,将MVVM与WPF一起使用的优势之一是双向数据绑定。 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? 1)将控件(视图)中的数据绑定到VM中的属性,以及2)集合或属性中的任何更改都反映在视图中的功能是否具有这种功能? 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. 这可能是一个非常简单的疑问,但是我对WPF还是陌生的,它是术语,现在已经远离WinForms。

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. 您已将视图与其ViewModel连接起来,因此您对其中一个所做的每个更改都会反映到另一个视图中。
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. 要获得这种行为,您必须在ViewModel中实现接口INotifyPropertyChange或使用“依赖关系属性”,而不是常规的CLR属性。
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 问候

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. 通常的想法是,不仅对UI中反映的DataContext所做的任何更改,而且对UI所做的更改也都被推回到DataContext中。

So you are right :) 所以你是对的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM