Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Binding.RelativeSource Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Gets or sets the binding source by specifying its location relative to the position of the binding target.

Namespace:   System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)

public RelativeSource RelativeSource { get; set; }

Property Value

Type: System.Windows.Data.RelativeSource

A RelativeSource object specifying the relative location of the binding source to use. The default is null.

This property is commonly used to bind one property of an object to another property of the same object, or to define a binding in a style or template.

By default, bindings inherit the data context specified by the DataContext property, if one has been set. However, the RelativeSource property is one of the ways you can explicitly set the source of a Binding and override the inherited data context. For more information, see How to: Specify the Binding Source.

The Binding.ElementName and Binding.Source properties also enable you to set the source of the binding explicitly. However, only one of the three properties, ElementName, Source, and RelativeSource, should be set for each binding, or a conflict can occur. This property throws an exception if there is a binding source conflict.

For XAML information, see RelativeSource MarkupExtension.

The following example shows a style trigger that creates a ToolTip that reports a validation error message. The value of the setter binds to the error content of the current TextBox (the TextBox using the style) using the RelativeSource property. For more information on this example, see How to: Implement Binding Validation.

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
  <Style.Triggers>
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
    </Trigger>
  </Style.Triggers>
</Style>

To see the full example, see Binding Validation Sample.

.NET Framework
Available since 3.0
Silverlight
Available since 3.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show:
© 2017 Microsoft