Free tools Windows power users keep installed
One-click scans. No signup required.
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.
Recommended Free Tools
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.
#1 Best Overall
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.
Do these 3 things before closing this tab:
1Fix the driver behind crashes, sound loss and screen glitches2Repair Windows errors before they cause bigger problems3Scan for outdated or missing drivers - takes under a minuteChoose 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.
Rank #3
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
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.
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
Best Value
- 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, orRelativeSource; 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.

