Learn about WPF-element binding and wpf Element Binding
ConceptExtract some information from the source object and use the information to set the attributes of the target object.
Example
|
ElementName |
Specify Source Element |
Path |
Attribute in the Source Element |
Mode |
OneWay: when the source attribute changes, update the target attribute TwoWay: when the source attribute changes, update the target attribute, and when the target attribute changes, update the source attribute OneTime: initially, the target attribute is set based on the source attribute value. All subsequent changes will be ignored. OneWayToSource: when the target attribute changes, the source attribute is updated. When the source attribute changes, the target attribute remains unchanged. Default: depends on the setting of the Target attribute. |
UpdateSourceTrigger |
If we bind an attribute value to the Text of a Text box, the value of the Target attribute will be updated only when the Text box loses focus; what if you want to update the value of the Target attribute in real time? In this case, you need to set the UpdateSourceTrigger attribute. This attribute has several possible situations: PropertyChange: Update LostFocus immediately when the target attribute is changed: when the target attribute changes, update Explicit when the target loses focus: Unless BindingExpression is called. updateSource () method. Otherwise, the source Default cannot be updated: The update behavior is determined based on the setting of the Target attribute. Most elements are PropertyChange settings, but TextBox. the Text attribute is set to LostFocus. |
Delay |
Sometimes the value of the Target attribute needs to be updated after a Delay of some time. In this case, you need to set the Delay attribute in milliseconds. |
Source |
Bound to a non-Element Object; mutually exclusive with ElementName This attribute refers to the reference to the source object (that is, the object that provides data) The following code binds an existing object RelativeSource |
Bound to a non-Element Object; mutually exclusive with ElementName Self: The expression is bound to another attribute of the same element. FindAncestor: The expression is bound to the parent element previusdata: bound to the previous data item in the data list, templateParent in the data list element: the element bound to the application template. This setting takes effect only in the template. |
Description
In addition to the attributes bound to the element, you can also bind the attributes of the element or attributes that exist in the indexer, for example, MyElement. Property. OtherPropertyMyElement. Property [2]. |
When it is bound to an element attribute with incorrect format, WPF does not compile and fail |
Two-way binding has more overhead than one-way binding, and one-time binding has less overhead than two-way binding and one-way binding. |
Try to use a clear binding mode instead of the Default binding mode. |
The source attribute is changed. After the target attribute is notified, the target attribute can also be used as the source attribute to notify the next target attribute. This means multiple bindings. |
Use code to create a bindingIn the above example, you can use C # code to create a binding code. The following code deletes the binding:If you want to delete the binding of an element through code, you can use the following two methods to complete code retrieval binding:
You can use the following method to obtain the binding object of an element and use DataContext to bind it.You can set the DataContext attribute for the parent element first, and then you can easily use the object pointed to by DataContext set by the parent element to modify the record.: Completed part of the content
ReferencesPro WPF 4.5 in C # 4th Edition