如何在WPF DataGrid中添加列ContextMenu [英] How to add a Column ContextMenu in the WPF DataGrid

查看:498
本文介绍了如何在WPF DataGrid中添加列ContextMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个上下文菜单添加到WPF数据网格的列中,并不太明白这是可能的。我知道如何添加一个到datagrid,但我想要有不同的菜单项基于列,以及菜单单击事件知道列或更好的单元格,选择上下文菜单。

I'm trying to add a context menu to a column in the WPF datagrid and don't quite see how this is possible. I know how to add one to the datagrid, but I would like to have different menu items based on the column as well have the menu click event be aware of column or better yet the cell that the context menu was chosen for.

我的最终目标是创建一个具有清除菜单项的上下文菜单,将用于清除该列中的数据。在一个复选框列的情况下,我不想要一个空字符串或者是false,我希望底层数据被清空。如果任何人有一个可重用的方法来建议,这将是非常感谢。

My ultimate goal is to create a context menu that has a "Clear" menu item which will be used to null out the data in that column. I don't want an empty string or false in the case of a check box column, I want the underlying data to be nulled out. If anyone has a suggestion on a reusable way to do this that would be much appreciated as well.

非常感谢!

推荐答案

另一个选项是将列标题设置为 TextBlock (或可以处理 ContextMenu )。

Another option is to set the column headers to a TextBlock (or some other control that can handle a ContextMenu).

所以你可以说:

// Create a context menu
var cm = new ContextMenu();
cm.Items.Add(new MenutItem{Header="SampleItem"});

// Create a textblock with your header text and link the context menu
var tb = new TextBlock{Text="My Column Name"};
tb.ContextMenu = cm;

// Set the grid column header to your textblock
grid.Columns[0].Header=tb;

这篇关于如何在WPF DataGrid中添加列ContextMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆