RelativeSource useful
Here are some of the ways
RelativeSource can be used:
To make the source element equal the target element:
{Binding RelativeSource={RelativeSource Self}}
To make the source element equal the target element’s TemplatedParent (a property
discussed in the next chapter):
{Binding RelativeSource={RelativeSource TemplatedParent}}
To make the source element equal the closest parent of a given type:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type
desiredType}}}
To make the source element equal the nth closest parent of a given type:
{Binding RelativeSource={RelativeSource FindAncestor,
AncestorLevel=n, AncestorType={x:Type desiredType}}}
To make the source element equal the previous data item in a data-bound collection:
{Binding RelativeSource={RelativeSource PreviousData}}
RelativeSource is especially useful for control templates, discussed in the next chapter.
But using RelativeSource with a mode of Self is handy for binding one property of an
element to another without having to give the element a name. An interesting example is the
following Slider whose ToolTip is bound to its own value:
<Slider ToolTip=”{Binding RelativeSource={RelativeSource Self}, Path=Value}”/>
RelativeSource can be used:
To make the source element equal the target element:
{Binding RelativeSource={RelativeSource Self}}
To make the source element equal the target element’s TemplatedParent (a property
discussed in the next chapter):
{Binding RelativeSource={RelativeSource TemplatedParent}}
To make the source element equal the closest parent of a given type:
{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type
desiredType}}}
To make the source element equal the nth closest parent of a given type:
{Binding RelativeSource={RelativeSource FindAncestor,
AncestorLevel=n, AncestorType={x:Type desiredType}}}
To make the source element equal the previous data item in a data-bound collection:
{Binding RelativeSource={RelativeSource PreviousData}}
RelativeSource is especially useful for control templates, discussed in the next chapter.
But using RelativeSource with a mode of Self is handy for binding one property of an
element to another without having to give the element a name. An interesting example is the
following Slider whose ToolTip is bound to its own value:
<Slider ToolTip=”{Binding RelativeSource={RelativeSource Self}, Path=Value}”/>