zoukankan      html  css  js  c++  java
  • WPF Datagrid 点击选择item

     public static object GetElementFromPoint(System.Windows.Controls.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)System.Windows.Media.VisualTreeHelper.GetParent(element);
                }
                return null;
            }
    
    
    private void Datagrid_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
      var item = GetElementFromPoint((ItemsControl)sender, e.GetPosition((ItemsControl)sender));      
    }
    

    或者绑定的是DataTable

     private void DataGrid1_MouseDoubleClick(object sender, MouseButtonEventArgs e)
            {
                DataGrid datagrid = sender as DataGrid;
                Point aP = e.GetPosition(datagrid);
                IInputElement obj = datagrid.InputHitTest(aP);
                DependencyObject target = obj as DependencyObject;
    
                while (target != null)
                {
                    if (target is DataGridRow)
                    {
                        //var s = this.dataGrid1.SelectedItem;
                        DataRow row = (this.dataGrid1.SelectedItem as DataRowView).Row;
                        if (row != null)
                        {
                            UInt64 ukey = Convert.ToUInt64(row["ukey"]);
                            byte[] content = SqlExManager.GetDpuProtectContent(ukey, _dpu.ThisInpC.Sqlmodel);
                            if (content != null)
                            {
                                ProtectTemapDll.ProtectTempClass.OpenProtecTemp(content, false);
                            }
                            else
                            {
                                MessageBox.Show("查询不到数据");
                            }
                        }
                    }
                    target = VisualTreeHelper.GetParent(target);
                }
            }
    

      

      

  • 相关阅读:
    MySQL-InnoDB锁(一)
    Java方法调用机制
    并发编程-锁相关的内存语义
    Java开发工具汇总
    并发编程-底层实现原理
    并发编程-Java内存模型
    JsonTest
    PTA(Basic Level)1034.有理数四则运算
    PTA(Advanced Level)1081.Rational Sum
    PTA(Advanced Level)1008.Elevator
  • 原文地址:https://www.cnblogs.com/wangyonglai/p/13711154.html
Copyright © 2011-2022 走看看