So I've finally found a solution to my problem here. But I still don't understand why CommandParameters is not updating when I try to use:
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridColumnHeader}}, Path=Column.SortMemberPath}"
So here is the code that works for me:
<DataGrid.Resources>
<ContextMenu x:Key="DataGridColumnHeaderMenu">
<MenuItem Name="mi_group" Header="Make Groups"
Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.GroupColumn}"
CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.Column.SortMemberPath}">
<MenuItem.Icon>
<Image Source="images/Treeview.png" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderMenu}" />
</Style></DataGrid.Resources>
The Advantage of using SortMemberPath in my case was that I could retrieve also a value (SelectedValuePath) for DataGridComboxColumn. Assumed that SelectedValuePath is equal to SortMemberPath which is in my case.
ViewModel:
private RelayCommand groupColumn;
public ICommand GroupColumn
{
get
{
if (groupColumn == null)
groupColumn = new RelayCommand(
(param) =>
{
UngroupColumns.Execute(null);
// param contains SortMemberPath of the DataGridColumn
string groupPropertyName = param as string;
if (groupPropertyName != null)
{
MyData.GroupDescriptions.Add(new PropertyGroupDescription(groupPropertyName));
MyData.SortDescriptions.Add(new SortDescription(groupPropertyName, ListSortDirection.Ascending));
}
});
return groupColumn;
}
}