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: System.Windows (in System.Windows.dll)
The RelativeSource type exposes the following members.
| Name | Description | |
|---|---|---|
![]() ![]() | RelativeSource | Initializes a new instance of the RelativeSource class by using the default relative source mode. |
![]() ![]() | RelativeSource(RelativeSourceMode) | Initializes a new instance of the RelativeSource class by using the specified relative source mode. |
| Name | Description | |
|---|---|---|
![]() | AncestorLevel | 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. |
![]() | AncestorType | Gets or sets the type of ancestor to look for. |
![]() ![]() | Mode | Gets or sets a value that describes the location of the binding source relative to the position of the binding target. |
| Name | Description | |
|---|---|---|
![]() ![]() | Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
![]() ![]() | Finalize | Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.) |
![]() ![]() | GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
![]() ![]() | GetType | Gets the Type of the current instance. (Inherited from Object.) |
![]() ![]() | MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
![]() | ProvideValue | 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).) |
![]() ![]() | ToString | Returns a string that represents the current object. (Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | ISupportInitialize.BeginInit | Infrastructure. Signals the object that initialization is starting. |
![]() ![]() | ISupportInitialize.EndInit | Infrastructure. Signals the object that initialization is complete. |
For XAML usage of RelativeSource, see RelativeSource Markup Extension.
RelativeSource is a class that supports a XAML markup extension. Markup extensions are typically implemented when there is a requirement to escape attribute values to be other than literal values or to use type converters. All markup extensions in XAML use curly braces ({}) in their attribute syntax, which is the convention by which a XAML processor recognizes that a markup extension must process the attribute. For more information, see the "Markup Extensions" section of XAML Overview. The RelativeSource Markup Extension is specifically required for setting the Binding.RelativeSource property as a XAML attribute.
The following Silverlight 5 code example demonstrates the use of this class. In this example, a ListBox is bound to a collection of Customer objects. Each item in the list box is bound to a single Customer in the collection, and its visual appearance is defined by the ItemTemplate property. Because of the item-to-customer binding, the template can bind the TextBlock.Text property directly to the Customer.Name property without qualification. However, the FontSize property is bound to a property of the page-level data context, which is inaccessible from the Customer object.
For this reason, the FontSize binding uses the RelativeSource markup extension with AncestorType set to UserControl, and a binding path of DataContext.CustomFontSize. This means that the binding uses the nearest UserControl ancestor as the data source, then binds to the CustomFontSize property of the object assigned to the DataContext property. If there were multiple, nested UserControl instances and the template needed to bind to an even higher-level data source, it could specify an AncestorLevel value of 2 or more.
<UserControl x:Class="SL5DataBindingFeatures.RelativeSourceFindAncestorTestPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:SL5DataBindingFeatures" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.DataContext> <local:CustomerViewModel CustomFontSize="35"> <local:CustomerViewModel.Customers> <local:CustomerCollection> <local:Customer Name="Customer1"/> <local:Customer Name="Customer2"/> </local:CustomerCollection> </local:CustomerViewModel.Customers> </local:CustomerViewModel> </UserControl.DataContext> <Grid x:Name="LayoutRoot" Background="White"> <ListBox ItemsSource="{Binding Customers}"> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" FontSize="{Binding DataContext.CustomFontSize, RelativeSource={RelativeSource AncestorType=UserControl}}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </UserControl>
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.





