Quantcast
Viewing all articles
Browse latest Browse all 18858

(MVVM) - How To Bind to DataContext from ContextMenu within DataTemplate

Hey Everyone,

I have a ListBox that contains a bunch of business objects (Models).

My heirarchy is like this:

Window
-> ItemsControl {Groups}
--> Group
---> ListBox {People}
----> Person
-----> ContextMenu (Edit/Delete)


My Window's DataContext is my ViewModel containing the Edit/Delete Commands.

I've tried many different ways to bind to the Command on my Window's DataContext, such as:

This is within the DataTemplate for my "Person":

<MenuItemHeader="Edit"Command="{Binding Path=DataContext.EditResourceCommand, RelativeSource={RelativeSource AncestorType={x:Type v:ManageResourcesView}}}"/>

The error I receive is:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType=.UI.ViewModels.ManageResourcesViewModel', AncestorLevel='1''. BindingExpression:Path=DataContext.EditResourceCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

The weird thing is... my DataTemplate for my "Group" has the following, which works...

SelectedItem="{Binding Path=DataContext.SelectedResource, RelativeSource={RelativeSource AncestorType={x:Type v:ManageResourcesView}}}"


Any help or advice would be greatly appreciated.

 

 

 


My educational lectures on C# and .Net in general (ranging from beginner level topics to advanced topics): mariocatch.wordpress.com


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>