0

我已经定义了一个这样的数据网格::

 <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!!!!
         }
    }

我想知道我哪里错了!!请帮助我解决这个问题。如果我以错误的方式实现我的要求,也指导我以更好的方式做事:)

4

4 回答 4

1

嘿我解决了::我修改后的代码如下:::

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)
    {
          dg.ContextMenu.Visibility = Visibility.Visible;  //works
    }
    else
    {
          dg.ContextMenu.Visibility = Visibility.Collapsed; //works
     }
}
于 2010-10-28T06:09:18.040 回答
1

我认为这个版本更快,您不必设置上下文菜单的可见性。如果点击是在列标题上,只需取消点击事件。

private void PEGrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
    var depObj = (DependencyObject)e.OriginalSource;
    while ((depObj != null) && !(depObj is DataGridColumnHeader))
    {
        depObj = VisualTreeHelper.GetParent(depObj);
    }
    if (depObj is DataGridColumnHeader)
    {
        e.Handled = true;
    }
}
于 2012-12-12T11:35:50.307 回答
0

我在 DataGridCell 中有 RichTextBox 不是 VisualTree 元素。我的代码:

private void dgLog_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkContentElement fce = e.OriginalSource as FrameworkContentElement;
        DependencyObject depObj = e.OriginalSource as DependencyObject;
        DataGridCell dgc = null;
        DataGridRow dgr = null;
        try
        {
            while (fce != null && !(fce is DataGridCell))
            {
                depObj = fce.Parent;
                fce = depObj as FrameworkContentElement;
            }
            while (depObj != null && !(depObj is DataGridRow))
            {
                if (depObj is DataGridCell)
                    dgc = depObj as DataGridCell;
                depObj = VisualTreeHelper.GetParent(depObj);
            }
            if (depObj == null)
                return;
            dgr = depObj as DataGridRow;
            if (((LogRecord)dgr.Item).SourceType == "FileMessage" && dgc.Column.DisplayIndex == 1)
                dgLog.ContextMenu = cm_dgLog;
            else
                dgLog.ContextMenu = null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(...);
            TraceHelper.TraceError(ex, GetType());
        }
    }//private void dgLog_PreviewMouseRightButtonDown

LogRecord 是 DataGridColumn.Item 的类型

于 2014-12-10T13:30:45.853 回答
-1
    private void dgLog_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        FrameworkContentElement fce = e.OriginalSource as FrameworkContentElement;
        DependencyObject depObj = e.OriginalSource as DependencyObject;
        DataGridCell dgc = null;
        DataGridRow dgr = null;
        try
        {
            while (fce != null && !(fce is DataGridCell))
            {
                depObj = fce.Parent;
                fce = depObj as FrameworkContentElement;
            }
            while (depObj != null && !(depObj is DataGridRow))
            {
                if (depObj is DataGridCell)
                    dgc = depObj as DataGridCell;
                depObj = VisualTreeHelper.GetParent(depObj);
            }
            if (depObj == null)
                return;
            dgr = depObj as DataGridRow;
            if (((LogRecord)dgr.Item).SourceType == "FileMessage" && dgc.Column.DisplayIndex == 1)
                dgLog.ContextMenu = cm_dgLog;
            else
                dgLog.ContextMenu = null;
        }
        catch (Exception ex)
        {
            MessageBox.Show(...);
            TraceHelper.TraceError(ex, GetType());
        }
    }//private void dgLog_PreviewMouseRightButtonDown

对不起。第一次尝试不太好。

于 2014-12-10T13:36:57.803 回答