If I use RelativeSource to get to the DataContext of the MainWindow, it doesn't work (binding errors about finding the source).
If I use ElementName to get to the DataContext of the MainWindow, same thing.
I also tried following this method to get at the correct DataContext: http://stackoverflow.com/questions/15033522/wpf-contextmenu-woes-how-do-i-set-the-datacontext-of-the-contextmenu.
See link below for a sample project showing my issue. I have placed a breakpoint on the command, so all you need to do is left-click the "convert to" button to get the context menu to appear and then click "to text". The breakpoint should be reached if the binding is correct. The method that is used in the attached project uses the stackoverflow method, and doesn't have any binding errors, but the command is still not reached.
http://1drv.ms/1F2k5ZH
Thanks for the help!
Jon
Awesome! Thanks for your help!
I've attached an updated version of your sample which uses the approach I just talked about. I took the command out of ItemViewModel and moved it to TheViewModelClass. I then setup the bindings in XAML such that when the button is clicked, it will fire the command in TheViewModelClass and pass it the ItemViewModel instance associated with where the button is located.
I thought I tried that. I think that since the Behavior you gave me modifies the DataContext on the ContextMenu, the Tag binding then is looking at the Command's DataContext (the outer context). I've got it working now so I won't mess with it for awhile. If you really want to make an example of doing it the way you suggest, you may. But I probably won't get around to testing it for a few weeks at least.
Thanks!
Hi Jon,
Could you maybe make use of the placement target's Tag property? All FrameworkElements have a Tag property so in the behavior you can just pass the placementTarget.Tag property to the AssociatedObject.Tag. Then inside the XamMenuItem, you can bind the CommandParameter property to the XamContextMenu's Tag property. A RelativeSource FindAncestor binding would do the trick there. This way your view model doesn't need to pull anything out of the datacontext. The command will only get passed the information it needs.
I found a solution by using {x:Reference ElementName} and then pulling the DataContext out in the ViewModel. Not the prettiest but it works. Thanks!