WPF: three attributes of Binding: Source, RelativeSource, and ElementName

Source: Internet
Author: User
Document directory
  • ElementName
  • Source
  • RelativeSource
ElementName

TheElementNameProperty is used to reference an object by the name of the object. This is Special useful in XAML, where you can directly reference other elements defined in XAML.

Example

<StackPanel Background = "Blue">
<Button x: Name = "refButton" Background = "Orange"/>
<Button Background = "{Binding ElementName = refButton, Path = Background}"/>
</StackPanel>

 

Source

The Source property is used to specify an object reference on which the binding Path or XPath will be evaluated. The Source property is usually used when the object on which the Binding is set is known andDiffers from the DataContext.

 

Example:

<CustomClass1 Property1 = "{Binding Source = {x: Static DateTime. Now}, Path = Day}"/>

<CustomClass1 Property1 = "{Binding Source = {StaticResource AnotherElement}, Path = ActualWidth}"/>

 

RelativeSource

Binding RelativeSource={
    RelativeSource Mode=FindAncestor, AncestorType={x:Type ItemType}
}
The default attributeRelativeSourceIsModeProperty. A complete set of valid values is given here (from MSDN ):

  • PreviusdataAllows you to bind the previous data item (not that control that contains the data item) in the list of data items being displayed.

  • TemplatedParentRefers to the element to which the template (in which the data-bound element exists) is applied. This is similar to setting a TemplateBindingExtension and is only applicable if the Binding is within a template.

  • SelfRefers to the element on which you are setting the binding and allows you to bind one property of that element to another property on the same element.

  • FindAncestorRefers to the ancestor in the parent chain of the data-bound element. you can use this to bind to an ancestor of a specific type or its subclasses. this is the mode you use if you want to specify AncestorType and/or AncestorLevel.

Example:

One attribute of an element is bound to another attribute of the element.

{Binding Path = PathToProperty, RelativeSource = {RelativeSource Self }}

 

An attribute of an element is bound to the attribute of the parent element.

{Binding Path = PathToProperty, RelativeSource = {RelativeSource AncestorType = {x: Type typeOfAncestor }}}

 

The attributes of the elements in the Template are bound to the attributes of the Template user element.

{Binding Path = PathToProperty, RelativeSource = {RelativeSource TemplatedParent }}

{TemplateBinding Path = PathToProperty}

 

Related Article

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.