This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft Edge More info about Internet Explorer and Microsoft Edge
public ref class RelativeSource : System::Windows::Markup::MarkupExtension, System::ComponentModel::ISupportInitialize
[System.Windows.Markup.MarkupExtensionReturnType(typeof(System.Windows.Data.RelativeSource))]
public class RelativeSource : System.Windows.Markup.MarkupExtension, System.ComponentModel.ISupportInitialize
[<System.Windows.Markup.MarkupExtensionReturnType(typeof(System.Windows.Data.RelativeSource))>]
type RelativeSource = class
    inherit MarkupExtension
    interface ISupportInitialize
Public Class RelativeSource
Inherits MarkupExtension
Implements ISupportInitialize
Inheritance
RelativeSource

Examples

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)/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>

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); Dim myBinding As 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, GetType(ItemsControl), 2)

Remarks

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 .

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback .

Submit and view feedback for

This product