Cannot add binding inside DataTemplate to a property outside of it
See original GitHub issueHello. 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:
- Created 4 years ago
- Comments:10 (6 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
EDIT: I figured out what my problem was. Apparently, overriding
ContextMenu.ItemTemplate
does nothing. So instead I used this: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?Seems like a bug. Workaround:
0.8.0 isn’t a recent though.