有哪些 WPF 绑定模式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2305179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:30:42  来源:igfitidea点击:

What are the various WPF binding modes?

wpfdata-binding

提问by DATT OZA

I do not understand the various data binding modes in WPF, such as:

不明白WPF中的各种数据绑定模式,比如:

  • One-Way
  • Two-Way
  • One-Time
  • etc...
  • 单程
  • 双向
  • 一度
  • 等等...

What does each of these modes mean?

这些模式分别是什么意思?

When should they be used?

什么时候应该使用它们?

回答by Dave

  • OneWay: Use this when you want the bound property to update the user interface.
  • TwoWay: This has the same behavior as OneWayand OneWayToSourcecombined. The bound property will update the user interface, and changes in the user interface will update the bound property (You would use this with a TextBoxor a Checkboxfor example.)
  • OneTime: This has the same behavior as OneWayexcept it will only update the user interface one time. This should be your default choice for binding (for various reasons I won't elaborate on here), you should only use other types of bindings if you actually need the extra functionality.
  • OneWayToSource: This is the opposite of OneWay-- user interface value changes update the bound property.
  • OneWay:当您希望绑定属性更新用户界面时使用此选项。
  • TwoWay:这OneWayOneWayToSource组合具有相同的行为。绑定属性将更新用户界面,用户界面中的更改将更新绑定属性(例如,您可以将其与 aTextBox或 a一起使用Checkbox。)
  • OneTime:这与OneWay它的行为相同,只是它只会更新用户界面一次。这应该是您绑定的默认选择(出于各种原因,我不会在这里详细说明),如果您确实需要额外的功能,您应该只使用其他类型的绑定。
  • OneWayToSource: 这与OneWay-- 用户界面值更改更新绑定属性相反。

If you don't specify anything, then the behavior will depend on the control that you are using.

如果您不指定任何内容,则行为将取决于您使用的控件。

For more info, see BindingModeenum on MSDN.

有关更多信息,请参阅BindingModeMSDN 上的枚举

回答by James

A binding consists of two entities:

绑定由两个实体组成:

  1. The Source (Typically the ViewModel in MVVM scenarios)
  2. The Target (The UI control)
  1. 源(通常是 MVVM 场景中的 ViewModel)
  2. 目标(UI 控件)

The target has to be a DependencyObject(for binding to work) and the source can be either a DependencyObjector it should have some mechanism to imitate the WPF Binding system about it being changed (Implemeting INotifyPropetyChnagedinterface).

目标必须是一个DependencyObject(为了绑定工作),源可以是一个,DependencyObject或者它应该有一些机制来模仿 WPF 绑定系统关于它被更改(INotifyPropetyChnaged实现接口)。



MVVM recommends the ViewModel project to be free from any View related references and hence it is recommended to use INotifyPropertyChangedinterface to make the Source object being heard by WPF binding. Binding happens between a property of Sourceand a property of Target(has to be a DependencyProperty). e.g. The TextPropertyof the TextBoxclass is DataBound to (say) UserNameproperty of the view model. WPF binding offers four types of Binding. Remember, Binding runs on UI thread unless otherwise you specify it to run otherwise.

MVVM 建议 ViewModel 项目不受任何与 View 相关的引用,因此建议使用INotifyPropertyChangedinterface 使 Source 对象被 WPF 绑定听到。绑定发生在Source的属性和Target的属性之间(必须是 a DependencyProperty)。例如TextPropertyTextBox该类的数据绑定到UserName视图模型的(比如说)属性。WPF 绑定提供四种类型的绑定。请记住,绑定在 UI 线程上运行,除非您指定它以其他方式运行。

  1. OneWay: The target propertywill listen to the source property being changed and will update itself. If you programmatically change the ViewwModel's UserNameproperty, it will reflect in the text box. This is of intermediate cost as the binding system watches only Sourcefor changes.
  2. TwoWay: The target property will listen to the source property being changed and will update itself. AND The source property will listen to the target property being changed and will update itself. Both the TextPropertyand the UserNameproperty will remain in sync and will update each other if one changes. This is most costly as the binding system has to watch both sides for change.
  3. OneWayToSource: The Source property will change if the target property is changed. If the user changes the TextProperty, the UserNameproperty will take up the changed value. This again is of intermediate cost as the binding system watches only Targetfor changes.
  4. OneTime: This happens only once during the lifetime of Binding, the Targetproperty will be updated with the Sourceproperty when the Binding happens. This is least costly and is advisable for scenarios where you have static data to be shown e.g. Label, TextBlocketc.
  1. OneWay目标属性将侦听正在更改的源属性并自行更新。如果您以编程方式更改 ViewwModel 的UserName属性,它将反映在文本框中。这是中间成本,因为绑定系统只监视 Source的变化。
  2. TwoWay:目标属性将侦听正在更改的源属性并自行更新。AND 源属性将侦听正在更改的目标属性并自行更新。无论是TextPropertyUserName属性将保持同步,如果一个变化将更新对方。这是最昂贵的,因为绑定系统必须观察双方的变化。
  3. OneWayToSource:如果目标属性更改,则源属性将更改。如果用户更改TextProperty,该UserName属性将占用更改后的值。这也是中间成本,因为绑定系统只监视 Target的更改。
  4. OneTime:这在 Binding 的生命周期内只发生一次,当 Binding 发生时,Target属性将使用Source属性更新。这是成本最低的,并且对于您要显示静态数据的场景(例如LabelTextBlock等)是可取的。

If you don't mention anything, every targetproperty has a default binding mode associated with itself. E.g. the TextPropertyof a TextBoxhas default binding mode as TwoWay. For the TextPropertyof a TextBlockit is one way.

如果您什么都不说,每个目标属性都有一个与其自身关联的默认绑定模式。例如TextPropertya 的TextBox默认绑定模式为TwoWay。对于TextPropertyaTextBlock它是一种方式。

It is advisable that you choose the right mode as it can help you reduce the application latency especially in cases where you have large number of controls in your UI.

建议您选择正确的模式,因为它可以帮助您减少应用程序延迟,尤其是在您的 UI 中有大量控件的情况下。

For more on MVVM hereis an article written by me.

欲了解更多关于MVVM这里是我写的一篇文章。