Binding.RelativeSource property

Applies to Windows and Windows Phone

Gets or sets the binding source by specifying its location relative to the position of the binding target. This is most often used in bindings within XAML control templates.

Syntax


public RelativeSource RelativeSource { get; set; }


<Binding RelativeSource="{RelativeSource TemplatedParent}"/>
-or-
<Binding RelativeSource="{RelativeSource Self}"/>


XAML Values

{RelativeSource TemplatedParent}

The control where a ControlTemplate is applied is the source for this binding. This is useful for applying validation error information in bindings at the template level.

{RelativeSource Self}

The target element should be used as the source for this binding. This is useful for binding one property of an element to another property on the same element.

Property value

Type: RelativeSource

The relative location of the binding source to use. The default is null.

Remarks

Source, RelativeSource, and ElementName are mutually exclusive in a binding. If you have set one of these attributes, then setting either of the other two in a binding (through XAML or through code) will cause an exception. Setting RelativeSource in XAML always requires the use of the RelativeSource markup extension. This is also true if you are creating the entire binding as a Binding markup extension, in which case the RelativeSource markup extension is nested within the RelativeSource component of the expression.

You can't set the property values of a Binding object after that binding has been attached to a target element and target property. If you attempt this you'll get a run-time exception.

Examples

This XAML example is taken from the generic.xaml file that defines the default visual states for all XAML controls. This particular segment is one of the visual states for the ToggleSwitch control. Here, one of the animations defined by the visual state references properties of the control that exist specifically so that templates can access them and get run-time values that modify the animation. For the template XAML to get properties from the control where the template is applied, the binding must use {RelativeSource TemplatedParent} as the RelativeSource value.


<VisualStateGroup x:Name="ToggleStates">
  <VisualStateGroup.Transitions>
    <VisualTransition x:Name="DraggingToOnTransition"
      From="Dragging"
      To="On"
      GeneratedDuration="0">
      <Storyboard>
        <RepositionThemeAnimation TargetName="SwitchKnob" 
          FromHorizontalOffset="
          {Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.KnobCurrentToOnOffset}"
        />
        <RepositionThemeAnimation TargetName="SwitchCurtain"
          FromHorizontalOffset="
          {Binding RelativeSource={RelativeSource TemplatedParent}, Path=TemplateSettings.CurtainCurrentToOnOffset}"
        />
      </Storyboard>
    </VisualTransition>
...
  </VisualStateGroup.Transitions>
</VisualStateGroup>

Requirements

Minimum supported client

Windows 8

Minimum supported server

Windows Server 2012

Minimum supported phone

Windows Phone 8.1 [Windows Runtime apps only]

Namespace

Windows.UI.Xaml.Data
Windows::UI::Xaml::Data [C++]

Metadata

Windows.winmd

See also

Binding
XAML data binding sample
Data binding overview
Binding markup extension
RelativeSource markup extension

 

 

Show:
© 2014 Microsoft