RelativeSource Class

RelativeSource Class

 

Implements a markup extension that describes the location of the binding source relative to the position of the binding target.

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

System.Object
  System.Windows.Markup.MarkupExtension
    System.Windows.Data.RelativeSource

[MarkupExtensionReturnTypeAttribute(typeof(RelativeSource))]
public class RelativeSource : MarkupExtension, ISupportInitialize

NameDescription
System_CAPS_pubmethodRelativeSource()

Initializes a new instance of the RelativeSource class.

System_CAPS_pubmethodRelativeSource(RelativeSourceMode)

Initializes a new instance of the RelativeSource class with an initial mode.

System_CAPS_pubmethodRelativeSource(RelativeSourceMode, Type, Int32)

Initializes a new instance of the RelativeSource class with an initial mode and additional tree-walking qualifiers for finding the desired relative source.

NameDescription
System_CAPS_pubpropertyAncestorLevel

Gets or sets the level of ancestor to look for, in FindAncestor mode. Use 1 to indicate the one nearest to the binding target element.

System_CAPS_pubpropertyAncestorType

Gets or sets the type of ancestor to look for.

System_CAPS_pubpropertyMode

Gets or sets a RelativeSourceMode value that describes the location of the binding source relative to the position of the binding target.

System_CAPS_pubpropertySystem_CAPS_staticPreviousData

Gets a static value that is used to return a RelativeSource constructed for the PreviousData mode.

System_CAPS_pubpropertySystem_CAPS_staticSelf

Gets a static value that is used to return a RelativeSource constructed for the Self mode.

System_CAPS_pubpropertySystem_CAPS_staticTemplatedParent

Gets a static value that is used to return a RelativeSource constructed for the TemplatedParent mode.

NameDescription
System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodProvideValue(IServiceProvider)

Returns an object that should be set as the value on the target object's property for this markup extension. For RelativeSource, this is another RelativeSource, using the appropriate source for the specified mode. (Overrides MarkupExtension.ProvideValue(IServiceProvider).)

System_CAPS_pubmethodShouldSerializeAncestorLevel()

Indicates whether the AncestorLevel property should be persisted.

System_CAPS_pubmethodShouldSerializeAncestorType()

Indicates whether the AncestorType property should be persisted.

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

NameDescription
System_CAPS_pubinterfaceSystem_CAPS_privmethodISupportInitialize.BeginInit()

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

System_CAPS_pubinterfaceSystem_CAPS_privmethodISupportInitialize.EndInit()

This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code.

One of the ways to specify a binding source is to use the Binding.RelativeSource property. This is the property type of that property. RelativeSource is simultaneously a markup extension implementation and a data object. When the extension returns itself (see ProvideValue), the relevant information is contained in the data. The main purpose of the markup extension is to allow a variable-argument constructor syntax in attribute form, so that the FindAncestor mode can be defined inline with the two extra arguments for ancestor type and level that the other modes do not require.

For XAML information, see RelativeSource MarkupExtension.

The following example shows a style trigger that creates a ToolTip that reports a validation error message. Using the RelativeSource property, the value of the setter binds to the error content of the current TextBox (the TextBox using the style). 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>

The following example shows the Style definition of a custom control called NumericUpDown. The Text property of the TextBlock is bound to the Value of the object that is the TemplatedParent, which is the NumericUpDown control that this Style is applied to in this case.

<!--ControlTemplate for NumericUpDown that inherits from
    Control.-->
<Style TargetType="{x:Type local:NumericUpDown}">
  <Setter Property="HorizontalAlignment" Value="Center"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
        <Grid Margin="3">
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
          <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
          </Grid.ColumnDefinitions>

          <Border BorderThickness="1" BorderBrush="Gray" 
                  Margin="2" Grid.RowSpan="2" 
                  VerticalAlignment="Center" HorizontalAlignment="Stretch">

            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}" 
                       Width="60" TextAlignment="Right" Padding="5"/>
          </Border>

          <RepeatButton Command="{x:Static local:NumericUpDown.IncreaseCommand}"  
                        Grid.Column="1" Grid.Row="0">Up</RepeatButton>

          <RepeatButton Command="{x:Static local:NumericUpDown.DecreaseCommand}"
                        Grid.Column="1" Grid.Row="1">Down</RepeatButton>

        </Grid>

      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

For the complete sample, see NumericUpDown Custom Control with Theme and UI Automation Support Sample.

The following returns the second ItemsControl encountered on the upward path starting at the target element of the binding.

Binding myBinding = new Binding();
// Returns the second ItemsControl encountered on the upward path
// starting at the target element of the binding
myBinding.RelativeSource = new RelativeSource(
    RelativeSourceMode.FindAncestor, typeof(ItemsControl), 2);

.NET Framework
Available since 3.0
Silverlight
Available since 3.0
Windows Phone Silverlight
Available since 7.0

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2016 Microsoft