欢迎光临
我的个人博客网站

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

  

  

赞(0) 打赏
未经允许不得转载:张拓的天空 » WPF Datagrid 点击选择item
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏