Pregunta

I have a DataGrid with different Columns (TextBox, Combobox, DataPicker, etc.) displaying Data via MVVM. Now I want to have ContextMenu on each ColumnHeader to Group by the selected column, but I can't access the Binding Source (Binding Path) of the Column to tell ViewModell which column it has to Group. When I open the context menu for the first time it works fine, but when I open the context menu once again, it still passes the SortMemberPath from the first time. What am I doing wrong?

XAML:

<DataGrid.Resources>
    <ContextMenu x:Key="DataGridColumnHeaderMenu">
        <MenuItem Name="mi_group" Header="Grouping"                                             
                  Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}, Path=DataContext.GroupColumn}"
                  CommandParameter="{Binding Path=Column.SortMemberPath, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridColumnHeader}}">
        </MenuItem></ContextMenu>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderMenu}" />
    </Style>
</DataGrid.Resources>

ViewModel:

public ICommand GroupColumn
{
    get
    {
        if (groupColumn == null)
        {
            groupColumn = new RelayCommand( (param) => {

                    UngroupColumns.Execute(null);
                    string groupPropertyName = param as string;   ///Don't want this Switch. ViewModel doesn't know name of the header

                    switch (groupPropertyName)
                    {
                        case "Project": {groupPropertyName = "project_id"; break;}
                        case "Date":    {groupPropertyName = "date";       break;}
                        case "Person":  {groupPropertyName = "person_id";  break;}
                        default:        {groupPropertyName = null; break;        }
                    }

                    if (groupPropertyName != null)
                    {
                        MyData.GroupDescriptions.Add(new PropertyGroupDescription(groupPropertyName));
                        MyData.SortDescriptions.Add(new SortDescription(groupPropertyName, ListSortDirection.Ascending));
                    }
                });
        }

        return groupColumn;
    }
}
¿Fue útil?

Solución

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;
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top