private void datagrid_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
    {
        object item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender));
    }

    private object GetElementFromPoint(ItemsControl itemsControl, Point point)
    {
        UIElement element = itemsControl.InputHitTest(point) as UIElement;
        while (element != null)
        {
            if (element == itemsControl)
                return null;
            object item = itemsControl.ItemContainerGenerator.ItemFromContainer(element);
            if (!item.Equals(DependencyProperty.UnsetValue))
                return item;
            element = (UIElement)VisualTreeHelper.GetParent(element);
        }
        return null;
    }

可以实现根据选中datagrid中对象属性的不同弹出不同的右键菜单:

if(item.type=="文件")

{

           ContextMenu textmenu = new ContextMenu();
            MenuItem item = new MenuItem();
            item .Header = "剪切";
            ClearText.Click += new RoutedEventHandler(btnCut_Click);
            datagrid.ContextMenu = textmenu;}

else if(item.type=="文件夹")

{……}

相关文章:

  • 2022-01-15
  • 2021-09-07
  • 2021-10-20
  • 2022-02-23
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-09
  • 2022-12-23
  • 2022-12-23
  • 2022-02-10
相关资源
相似解决方案