Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

Binding to the Parent DataContext from Inside a WPF DataTemplate

A WPF DataTemplate inherits the current item as its DataContext. To reach a containing control’s context, use RelativeSource FindAncestor and bind through DataContext.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Inside a WPF DataTemplate, DataContext normally refers to the item being displayed. To bind to a property on a containing control’s data context, use RelativeSource FindAncestor to locate that control, then start the binding path with DataContext:

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

Replace ListBox with the actual ancestor control containing the templated element. This keeps item properties and parent-view properties distinct rather than changing the template’s data context.

As an Amazon Associate I earn from qualifying purchases.

Why a DataTemplate sees the item, not the parent context

A data template presents an object, so elements created from it generally inherit that object as their DataContext. A binding such as {Binding ItemProperty} therefore looks for ItemProperty on the current item. This is the right pattern when the value belongs to the item.

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

When a template also needs a value from a containing control’s view model or data object, specify a source explicitly. Microsoft’s WPF data binding overview describes inherited binding context and binding sources; the RelativeSource MarkupExtension documentation describes finding an ancestor by type.

Bind to a containing control’s DataContext

Use RelativeSource in FindAncestor mode and set AncestorType to the control you want. The path then goes through that control’s DataContext to the desired property:

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

For example, if a ListBox is bound to a collection of items and its surrounding view model exposes a command, a button inside an item template can bind to that command through the ListBox:

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Name}" />
                <Button Content="Open"
                        Command="{Binding DataContext.OpenCommand,
                            RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}"
                        CommandParameter="{Binding}" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Here, Name and the command parameter come from the current item, while OpenCommand comes from the ListBox data context. Using separate bindings makes each source explicit.

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.

Choose the source pattern that matches the target

What the binding should read Pattern When to use it
A property on the item being displayed {Binding ItemProperty} The template normally inherits the current item as its context.
A property on a containing control’s data context {Binding DataContext.Property, RelativeSource={RelativeSource AncestorType={x:Type SomeControl}}} Use when the target element can find that control in its ancestor chain.
A property on the control to which a control template is applied {Binding Property, RelativeSource={RelativeSource TemplatedParent}} Use for the specific control-template relationship; it is not a general way to find any outer control.
A property on an outer instance of a repeated ancestor type Add AncestorLevel=n Use when matching controls are nested and the nearest matching ancestor is not the intended one.

The Microsoft RelativeSource documentation defines FindAncestor as locating an ancestor by type and AncestorLevel as selecting among matching ancestors. For example:

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

Use a level only after confirming the ancestor ordering in the actual element tree. The target must have the intended control above it in that tree.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When the target is in a DataGrid

Distinguish a rendered element inside a cell from a column definition. Microsoft’s DataGrid documentation explains that rows bind to source objects and columns bind to properties of those objects; a DataGridTemplateColumn supplies template-defined cell content. An element instantiated inside a cell can use ancestor lookup when its actual element tree contains the intended DataGrid. The column property itself is not that rendered element, so do not assume it can use the same ancestor lookup.

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
Rank #4
Sale
Programming WPF
  • Used Book in Good Condition

Diagnose a binding that does not resolve

  • Check the target: Confirm the binding is on an element instantiated inside the data template, not on a disconnected object such as a column definition.
  • Check the ancestor type: Use a type that actually appears between the target and the intended owner in the ancestor chain.
  • Check nested controls: If several ancestors have the same type, verify which one is selected and specify AncestorLevel when needed.
  • Check the path: If the property belongs to the ancestor’s context, include DataContext, as in DataContext.SomeProperty.
  • Use one explicit binding source: A binding should use at most one of ElementName, Source, or RelativeSource. Combining them can produce a conflict; see Microsoft’s binding declarations documentation.
  • Treat disconnected targets as a structural problem: Ancestor lookup depends on an ancestor relationship. The cited WPF documentation does not establish one universal proxy or workaround for every disconnected target 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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.