Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Inside a WPF DataTemplate, use RelativeSource FindAncestor to reach a containing control’s DataContext. For example, to read a property named SomeProperty from the nearest ListBox:

<TextBlock Text="{Binding DataContext.SomeProperty,
    RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />

Replace ListBox with the ancestor control that actually contains the template element. A template normally inherits the current item as its DataContext; use the explicit ancestor binding only when the source should be a containing control instead.

Why a template binding sees the item, not the parent context

A DataTemplate presents a data object, so elements inside it normally inherit that object as their DataContext. A binding such as {Binding ItemProperty} therefore reads from the item being displayed. This is expected behavior, not a failure to inherit the parent view model.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When the template also needs a property from a containing control’s context, make the source explicit: find the control in the ancestor chain, then traverse through its DataContext.

Bind to a containing control’s DataContext

Use RelativeSource FindAncestor and specify the intended control type with AncestorType:

<TextBlock Text="{Binding DataContext.SomeProperty,
    RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" />

The path starts with DataContext because the source object found by the ancestor lookup is the ListBox; SomeProperty belongs to that control’s context. Microsoft documents FindAncestor for locating an ancestor by type and AncestorLevel for selecting among matching ancestors. See WPF data binding overview and RelativeSource MarkupExtension – WPF.

The ancestor type must match the control actually in the target element’s ancestor chain. If the template is hosted by a different control, replace ListBox with that host’s type.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose the source that matches the property

What the binding should read Binding pattern Why
A property on the current item {Binding ItemProperty} The template normally inherits the item as its context.
A property on a containing control’s context {Binding DataContext.Property, RelativeSource={RelativeSource AncestorType={x:Type SomeControl}}} The ancestor lookup finds the control; the path then accesses its context.
A property on the control to which a control template is applied {Binding Property, RelativeSource={RelativeSource TemplatedParent}} TemplatedParent represents the control that the template is applied to.

TemplatedParent is for the control-template relationship; it is not a general substitute for finding any outer control. For an ancestor lookup, Microsoft’s guidance describes FindAncestor as useful in control templates or predictable, self-contained UI compositions where an ancestor of the expected type is present.

Select the correct ancestor when controls are nested

If matching controls are nested, an ancestor type alone may find the nearer one rather than the outer one you intend. Add AncestorLevel to select the matching ancestor by its upward-search level:

<TextBlock Text="{Binding DataContext.SomeProperty,
    RelativeSource={RelativeSource FindAncestor,
        AncestorType={x:Type ListBox}, AncestorLevel=2}}" />

Choose the level based on the actual nesting order. The ancestor type and level together identify the source; an incorrect level can produce a valid binding to the wrong control.

Rank #4
Sale
Programming WPF
  • Used Book in Good Condition
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Account for DataGrid columns and generated elements

WPF’s DataGrid distinguishes between rows, which bind to source objects, and columns, which bind to properties of those objects. A DataGridTemplateColumn can define content through a template. These are different targets: an element generated inside a cell may be able to find the containing grid in its ancestor chain, while a column property itself is not that rendered element and should not be assumed to have the same tree relationship.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Check whether the binding is on an actual element instantiated in the cell template or on the column definition. The Microsoft DataGrid documentation describes the row, column, and template roles.

Quick Recap

SaleBestseller No. 3
SaleBestseller No. 4
Programming WPF
Programming WPF
Used Book in Good Condition
$34.82
Bestseller No. 5
WPF 4.5 Unleashed
WPF 4.5 Unleashed
Used Book in Good Condition
$12.73
Best Value
WPF 4.5 Unleashed
  • Used Book in Good Condition

Troubleshoot a binding that does not resolve

  • Check the target: Confirm the binding is on an element instantiated inside the DataTemplate, not on a disconnected object such as a column definition.
  • Check the ancestor type: Use a type present between the target element and the intended containing control.
  • Check nested controls: If the binding reaches the wrong matching ancestor, set and verify AncestorLevel.
  • Check the path: If the desired property belongs to the ancestor’s context, begin the path with DataContext..
  • Check explicit sources: A binding should specify at most one of ElementName, Source, or RelativeSource; combining these source declarations can conflict. See Binding sources overview.
  • Check the tree relationship: Ancestor lookup depends on an ancestor relationship. If the target is disconnected from the relevant element tree, treat that as a structural issue; no single proxy or workaround is established for every such arrangement.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.