zoukankan      html  css  js  c++  java
  • WPF datagrid 弹出右键菜单时先选中该项

     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=="文件夹")

    {……}

  • 相关阅读:
    java ppt4
    课后作业 2
    课后作业2
    java ppt题目
    CommandParameter设计
    《大道至简》第二章读后感
    8.多态
    7.接口与继承
    6.数组
    字符串
  • 原文地址:https://www.cnblogs.com/sjqq/p/8585760.html
Copyright © 2011-2022 走看看