禁用某些 DatGrid 单元格 WPF 上的上下文菜单

发布于 2024-09-29 15:59:24 字数 1772 浏览 7 评论 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!!!!
         }
    }

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

够运 2024-10-06 15:59:24

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

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

Hey I Solved it :: My modified code is as follows:::

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
     }
}
清晨说晚安 2024-10-06 15:59:24

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

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

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.

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;
    }
}
太阳公公是暖光 2024-10-06 15:59:24

我在 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 的类型

I have RichTextBox in DataGridCell that is not VisualTree element. My code:

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 is the Type of DataGridColumn.Item

眼角的笑意。 2024-10-06 15:59:24
    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

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

    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

Sorry. First attemt was not so good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文