从上下文菜单中获取单元格列

时间:2017-08-08 10:35:37

标签: c# wpf datagrid

到目前为止,我有:

private void FilterItem_Click(object sender, RoutedEventArgs e)
    {
        // Get the clicked MenuItem
        MenuItem menuItem = (MenuItem)sender;

        //Get the ContextMenu to which the menuItem belongs
        ContextMenu contextMenu = (ContextMenu)menuItem.Parent;

        //Find the placementTarget
        DataGridRow rowItem = (DataGridRow)contextMenu.PlacementTarget;

        DataRowView dr = (DataRowView)rowItem.DataContext;
        DataRow row = dr.Row;
        string value = dr[0].ToString();
        string col = row.Table.Columns[0].ColumnName;

        Console.WriteLine(rowItem);
        Console.WriteLine(value);
        Console.WriteLine(col);
        Console.WriteLine(e.OriginalSource);
        Console.WriteLine(e);
    }

这很好用,但我不想要0,我想要选择上下文菜单的单元格的列索引。从我的谷歌fu,我一直无法辨别提取列的具体方法。来自e的信息并不是特别有用;返回的两个值是System.Windows.Controls.MenuItem Header:Filter Items.Count:0 System.Windows.RoutedEventArgs

非常感谢任何帮助。

编辑:当前XAML

 <DataGrid x:Name="Grid" HorizontalAlignment="Stretch" Height="Auto" Margin="20,55,20,40" VerticalAlignment="Stretch" Width="Auto" ItemsSource="{Binding DataGridCollection.SourceCollection}">
        <DataGrid.Resources>
            <ContextMenu x:Key="DataGridColumnHeaderContextMenu">
                <MenuItem Header="Filter" Click="MenuItem_Click" />
            </ContextMenu>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}"/>
            </Style>
            <ContextMenu x:Key="rowContextMenu">
                <MenuItem Header="Filter" Click="FilterItem_Click"/>
            </ContextMenu>
        </DataGrid.Resources>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow" BasedOn="{StaticResource {x:Type DataGridRow}}">
                <Setter Property="ContextMenu" Value="{StaticResource rowContextMenu}" />
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>

有问题的上下文菜单是DataGridRow上的菜单

1 个答案:

答案 0 :(得分:0)

根据Maxim的评论答案:

将XAML更改为:

<DataGrid.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
            <Setter Property="ContextMenu" Value="{StaticResource rowContextMenu}" />
        </Style>
    </DataGrid.CellStyle>

在C#中:

        // Get the clicked MenuItem
        MenuItem menuItem = (MenuItem)sender;

        //Get the ContextMenu to which the menuItem belongs
        ContextMenu contextMenu = (ContextMenu)menuItem.Parent;

        //Find the placementTarget
        DataGridCell cellItem = (DataGridCell)contextMenu.PlacementTarget;

        DataGridColumn col = cellItem.Column;
        int index = Grid.Columns.IndexOf(col);

        DataRowView dr = (DataRowView)cellItem.DataContext;
        DataRow row = dr.Row;
        string value = dr.Row.ItemArray[index].ToString();
        string column = col.Header.ToString();

        Console.WriteLine(value);
        Console.WriteLine(column);