禁用某些 DatGrid 单元格 WPF 上的上下文菜单
我定义了一个像这样的数据网格 ::
<cc:PEDataGrid AutoGenerateColumns="False"
ItemsSource="{Binding Rows}"
Width="Auto"
PreviewMouseRightButtonDown="PEGrid_PreviewMouseRightButtonDown"
Loaded="CommonPEGrid_Loaded">
<wpfkit:DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Cut" />
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</ContextMenu>
</wpfkit:DataGrid.ContextMenu>
</cc:PEDataGrid>
当右键单击完成时,它会在每个单元格上显示 contextMenu。
我想禁用除标题之外的所有单元格的上下文菜单,以及某些条件下的标题。 (我不想使用 DataGridHeaderStyle,因为我不想在这里解释一些其他问题。)
我已经在数据网格上和我的处理程序中为 PreviewMouseRightButtonDown 定义了一个处理程序尝试做这样的事情::
private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DependencyObject depObj = (DependencyObject)e.OriginalSource;
while ((depObj != null) && !(depObj is DataGridColumnHeader))
{
depObj = VisualTreeHelper.GetParent(depObj);
}
if (depObj == null)
{
return;
}
if (depObj is DataGridColumnHeader)
{
//some condition here which says whether contextmenu is required on this header
(depObj as DataGridColumnHeader).ContextMenu = null;
//the above line is not working!!!!
}
else
{
(depObj as DataGridCell).ContextMenu = null;
//the above line not working!!!!
}
}
我想知道我哪里出错了!!请帮助我解决这个问题。如果我以错误的方式实现了我的要求,也指导我以更好的方式去做:)
I have defined a datagrid like this ::
<cc:PEDataGrid AutoGenerateColumns="False"
ItemsSource="{Binding Rows}"
Width="Auto"
PreviewMouseRightButtonDown="PEGrid_PreviewMouseRightButtonDown"
Loaded="CommonPEGrid_Loaded">
<wpfkit:DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="Cut" />
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</ContextMenu>
</wpfkit:DataGrid.ContextMenu>
</cc:PEDataGrid>
This shows contextMenu on every cell when right click is done.
I want to disable context menu for all the cells except headers and also on header for some condition. (I dont want to use DataGridHeaderStyle because of some other problems which I dont want to explain here.)
I have defined a handler for PreviewMouseRightButtonDown on the datagrid and in the handler I am trying to do something like this::
private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DependencyObject depObj = (DependencyObject)e.OriginalSource;
while ((depObj != null) && !(depObj is DataGridColumnHeader))
{
depObj = VisualTreeHelper.GetParent(depObj);
}
if (depObj == null)
{
return;
}
if (depObj is DataGridColumnHeader)
{
//some condition here which says whether contextmenu is required on this header
(depObj as DataGridColumnHeader).ContextMenu = null;
//the above line is not working!!!!
}
else
{
(depObj as DataGridCell).ContextMenu = null;
//the above line not working!!!!
}
}
I want to know where am I going wrong!! Please help me regarding this. Also guide me to do in a better way if I am achieving my requirement in a wrong way :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嘿我解决了::我修改后的代码如下:::
Hey I Solved it :: My modified code is as follows:::
我认为这个版本更快,而且您不必设置上下文菜单的可见性。如果单击的是列标题,只需取消单击事件即可。
I think this version is way faster and you don't have to set the visibility of the context menu. Just cancel the click event if the click was on the column header.
我在 DataGridCell 中有 RichTextBox,它不是 VisualTree 元素。我的代码:
LogRecord 是 DataGridColumn.Item 的类型
I have RichTextBox in DataGridCell that is not VisualTree element. My code:
LogRecord is the Type of DataGridColumn.Item
对不起。第一次尝试不太好。
Sorry. First attemt was not so good.