I need to create a ContextMenu where I pass a currently selected index to a ViewModel using the Datagrid CommandParameter. Want to do The following Xaml code does not work, what could be the problem
& Lt; / ContextMenu & gt; & Lt; / DG: DataGrid.ContextMenu & gt; & Lt; / DG: Data grid & gt;
A context menu is not part of the same view tree Ancestors' bindings do not work Because the context menu is not the child of the element that is running it; Datagrid in your case.
There are some ideas, I have already answered this question
But whatever you want is the placement goal to do something like this (unless ContextMenu DataContext = " Add a property (i.e. placement target)
& lt header = "at {Bindings RelativeSource = {RelativeSource mode = self}, path = PlacementTarget}"> <> MenuItem AddDividerCommand datagrid Separator "CommandParameter =" {binding path = SelectedIndex} "command =" {binding path = AddDividerCommand} "/>