我已经定义了一个这样的数据网格::
<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!!!!
}
}
我想知道我哪里错了!!请帮助我解决这个问题。如果我以错误的方式实现我的要求,也指导我以更好的方式做事:)