I do not understand the variou开发者_运维技巧s data binding modes in WPF, such as:
- One-Way
- Two-Way
- One-Time
- etc...
What does each of these modes mean?
When should they be used?
OneWay
: Use this when you want the bound property to update the user interface.TwoWay
: This has the same behavior asOneWay
andOneWayToSource
combined. The bound property will update the user interface, and changes in the user interface will update the bound property (You would use this with aTextBox
or aCheckbox
, for example.)OneTime
: This has the same behavior asOneWay
, except 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 ofOneWay
-- user interface value changes update the bound property.
If you don't specify anything, then the behavior will depend on the control that you are using.
For more info, see BindingMode
enum on Microsoft Docs.
A binding consists of two entities:
- The Source (Typically the ViewModel in MVVM scenarios)
- The Target (The UI control)
The target has to be a DependencyObject
(for binding to work) and the source can be either a DependencyObject
or it should have some mechanism to imitate the WPF Binding system about it being changed (Implemeting INotifyPropetyChnaged
interface).
MVVM recommends the ViewModel project to be free from any View related references and hence it is recommended to use INotifyPropertyChanged
interface to make the Source object being heard by WPF binding.
Binding happens between a property of Source and a property of Target (has to be a DependencyProperty
).
e.g. The TextProperty
of the TextBox
class is DataBound to (say) UserName
property 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.
- OneWay: The target property will listen to the source property being changed and will update itself. If you programmatically change the ViewwModel's
UserName
property, it will reflect in the text box. This is of intermediate cost as the binding system watches only Source for changes. - 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
TextProperty
and theUserName
property 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. - OneWayToSource: The Source property will change if the target property is changed. If the user changes the
TextProperty
, theUserName
property will take up the changed value. This again is of intermediate cost as the binding system watches only Target for changes. - OneTime: This happens only once during the lifetime of Binding, the Target property will be updated with the Source property when the Binding happens. This is least costly and is advisable for scenarios where you have static data to be shown e.g.
Label
,TextBlock
etc.
If you don't mention anything, every target property has a default binding mode associated with itself. E.g. the TextProperty
of a TextBox
has default binding mode as TwoWay
. For the TextProperty
of a TextBlock
it is one way.
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.
For more on MVVM here is an article written by me.
精彩评论