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.

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)
XML Namespace:

<MarkupExtensionReturnTypeAttribute(GetType(RelativeSource))> _
Public Class RelativeSource
	Inherits MarkupExtension
	Implements ISupportInitialize
Dim instance As RelativeSource

/** @attribute MarkupExtensionReturnTypeAttribute(System.Windows.Data.RelativeSource) */ 
public class RelativeSource extends MarkupExtension implements ISupportInitialize
public class RelativeSource extends MarkupExtension implements ISupportInitialize
 For XAML information, see RelativeSource MarkupExtension. 

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.

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 Implement Binding Validation.

<Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
    <Trigger Property="Validation.HasError" Value="true">
      <Setter Property="ToolTip"
        Value="{Binding RelativeSource={x:Static RelativeSource.Self},

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.

<Style TargetType="{x:Type local:NumericUpDown}">
  <Setter Property="HorizontalAlignment" Value="Center"/>
  <Setter Property="VerticalAlignment" Value="Center"/>
  <Setter Property="Template">
      <ControlTemplate TargetType="{x:Type local:NumericUpDown}">
        <Grid Margin="3">

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



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

More Code

How to: Create ListViewItems with a CheckBox

This example shows how to display a column of CheckBox controls in a ListView control that uses a GridView.


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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft