Learning wpf--Element Binding

Source: Internet
Author: User

ConceptExtracts some information from the source object and sets the properties of the target object with this information

Example

When assigning a value to the FontSize property of the TextBlock control, We used the binding expression data-binding expression to use the markup extension of XAML (and therefore have curly braces) (see:) Here you create a System.Windows.Data.Binding object, so the binding expression begins with the word binding, in this case, only two properties of the binding object are set to E Lementname and Path properties
ElementName Specify the source element
Path Specifying attributes in the source element
Mode OneWay: When the Source property changes, update the target property TwoWay: When the sourceWhen the property changes, the target property is updated, and when the target property changes, the Source property is updated OneTime:The target property is initially set based on the Source property value, and all subsequent changes are ignoredOneWayToSource:When the target property changes, the Source property is updated, and the Source property changes, the target property is unchangedDefault:Depends on the setting of the target property
UpdateSourceTrigger If we bind the value of a property to the text of a textbox, the value of the target property is updated only when the text box loses focus, and what happens if you want to update the value of the target property in real time? Then you need to setUpdateSourceTrigger attribute, there are several possible scenarios for this propertyPropertyChange:update immediately when the target attribute sounds changedLostFocus:update when the target property has changed and the target loses focusExplicit:the source cannot be updated unless the Bindingexpression.updatesource () method is calledDefault:depending on the setting of the target property to determine the update behavior, most elements are PropertyChange settings, but the TextBox.Text property is LostFocus settings
Delay Sometimes it takes a while to update the value of the target property, and you need to set the delay property in milliseconds
Source Binds to non-element objects; Mutex with elementname
This property points to a reference to the source object (that is, the object that provides the data)
The following code can be bound to an already existing object

The following code can be bound to a resource that already exists
RelativeSource binds to non-element objects; Mutex with ElementNameSelf :An expression is bound to another property of the same elementFindAncestor:binding an expression to a parent elementPreviousdata:Bind to the previous data item in the data list, apply in the data list element templateparent: The element that is bound to the application template, and this setting takes effect in the template

Description

In addition to attributes that are bound to an element, you can also bind to attributes of an element's properties or to an indexer's properties such as: Myelement.property.otherpropertymyelement.property[2]
WPF does not compile if it is bound to an improperly formed element property
Two-way binding is much more expensive than one-way binding, and onetime binding is less expensive than two-way binding and one-way binding
Try to use explicit binding mode instead of the default binding mode
The target property can also be used as the Source property to notify the next target property when the source property is changed, which is the multi-binding
creating bindings using codeIn the example above, the binding code can be created using C # code as follows using code to remove bindingsIf you delete a binding of an element as if it were through code, you can do it in two different ways

retrieving bindings using code

You can get the binding object for an element by using the following method Binding with DataContextYou can set the DataContext property on the parent element first, and then in the child element, you can easily use the object pointed to by the DataContext set by the parent element. Modify a record2015-1-5: Complete part of the content References"Pro WPF 4.5 in C # 4th Edition"

Learning wpf--Element Binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.