question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot add binding inside DataTemplate to a property outside of it

See original GitHub issue

Hello. I’m having a problem with binding to my ViewModel’s property inside DataTemplate in the View.

Consider the following example: I have a View and a ViewModel. In the View I have a button and an ItemsControl with defined DataTemplate. I assigned a Name to a View to make it possible to reach it’s DataContext from the DataTemplate. Here’s the code for binding outside of the DataTemplate <Button Command="{Binding ElementName=Root,Path=DataContext.DeleteCommand}">Delete</Button> where the Root is name of my View.

It works when placed outside of the DataTemplate, inside it won’t work.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
derekantricancommented, Oct 17, 2020

EDIT: I figured out what my problem was. Apparently, overriding ContextMenu.ItemTemplate does nothing. So instead I used this:

<Style Selector="MenuItem">
  <Setter Property="Template" Value="{StaticResource MenuItemPanelTemplate}"/>
  <Style.Resources>
    <ControlTemplate x:Key="MenuItemPanelTemplate">
      <Button Padding="0" BorderThickness="0" Background="Transparent"
              Command="{Binding $parent[Window].DataContext.AddItemToToolTips}">
        <TextBlock Text="{Binding .}" Margin="5"/>
      </Button>
    </ControlTemplate>
  </Style.Resources>
</Style>

What is the proper way to do this in 0.9.12? I’ve been trying a bunch of variations on this (binding DataContext to window’s DataContext, binding to the DataContext.Command with the element name of the parent, and the special # and $ usages mentioned here) with no luck. Here’s the section of my code where I’m trying to bind the inner Button’s command to a command on the same DataContext as the outer ContextMenu. Any suggestions?

<Button Margin="5">
  <Button.ContextMenu>
    <ContextMenu Items="{Binding MenuItems}" SelectedItem="{Binding SelectedMenuItem}">
      <ContextMenu.ItemTemplate>
        <DataTemplate>
          <Button Padding="0" BorderThickness="0" Background="Transparent" Command="{Binding $parent[ContextMenu].DataContext.AddItemToToolTips}">
            <TextBlock Text="{Binding .}"/>
          </Button>
        </DataTemplate>
      </ContextMenu.ItemTemplate>
    </ContextMenu>
  </Button.ContextMenu>
</Button>
1reaction
FoggyFindercommented, Jul 27, 2019

Seems like a bug. Workaround:

 Command="{Binding $parent[ItemsControl].DataContext.DeleteCommand}"

0.8.0 isn’t a recent though.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Outside Property inside a DataTemplate WPF
My initial thought is that I could just bind the ItemsSource to a Property in the window and that would return an ItemsSource;...
Read more >
{Binding} not working in ContentTemplate in Custom Control
Hello,. Welcome to Microsoft Q&A! The issue is that when you are using binding in the TextBlock, the TextBlock can't find the datacontext...
Read more >
DataTemplate Class (System.Windows)
In this example, the data object is a class called Task . Note that each TextBlock element in this template is bound to...
Read more >
Untitled
Binding To a Parent Element in UWP and WinUI 3 Magnus Montin Cannot add binding inside DataTemplate to a property outside … WebBut,...
Read more >
How do you bind the path in a datatemplate to ...
Edit: I just reread your question. You cannot store the name of the path in a variable. It does not work that way....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found