1. private void Dg_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
  2. {
  3. Console.WriteLine("start");
  4. foreach (DataGridCellInfo info in dg.SelectedCells)
  5. {
  6. FrameworkElement element = info.Column.GetCellContent(info.Item);
  7. string str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text;
  8. Console.WriteLine(str);
  9. }
  10. }
  1. private void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3. DataGridCellInfo info = new DataGridCellInfo(dg.Items[], dg.Columns[]);
  4. Console.WriteLine("start");
  5. FrameworkElement element = info.Column.GetCellContent(info.Item);
  6. string str = ((TextBlock)info.Column.GetCellContent(info.Item)).Text;
  7. Console.WriteLine(str);
  8.  
  9. }
  1. private void Button_Click(object sender, RoutedEventArgs e)
  2. {
  3. DataGridCell cell = dg.GetCell(, );
  4. TextBlock tb = cell.Content as TextBlock;
  5. Console.WriteLine(tb.Text);
  6. }
  7.  
  8. public static class DataGridExtension
  9. {
  10. /// <summary>
  11. /// 获取DataGrid控件单元格
  12. /// </summary>
  13. /// <param name="dataGrid">DataGrid控件</param>
  14. /// <param name="rowIndex">单元格所在的行号</param>
  15. /// <param name="columnIndex">单元格所在的列号</param>
  16. /// <returns>指定的单元格</returns>
  17. public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
  18. {
  19. DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
  20. if (rowContainer != null)
  21. {
  22. DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
  23. DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  24. if (cell == null)
  25. {
  26. dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]);
  27. cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex);
  28. }
  29. return cell;
  30. }
  31. return null;
  32. }
  33.  
  34. /// <summary>
  35. /// 获取DataGrid的行
  36. /// </summary>
  37. /// <param name="dataGrid">DataGrid控件</param>
  38. /// <param name="rowIndex">DataGrid行号</param>
  39. /// <returns>指定的行号</returns>
  40. public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)
  41. {
  42. DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  43. if (rowContainer == null)
  44. {
  45. dataGrid.UpdateLayout();
  46. dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]);
  47. rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex);
  48. }
  49. return rowContainer;
  50. }
  51.  
  52. /// <summary>
  53. /// 获取父可视对象中第一个指定类型的子可视对象
  54. /// </summary>
  55. /// <typeparam name="T">可视对象类型</typeparam>
  56. /// <param name="parent">父可视对象</param>
  57. /// <returns>第一个指定类型的子可视对象</returns>
  58. public static T GetVisualChild<T>(Visual parent) where T : Visual
  59. {
  60. T child = default(T);
  61. int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
  62. for (int i = ; i < numVisuals; i++)
  63. {
  64. Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
  65. child = v as T;
  66. if (child == null)
  67. {
  68. child = GetVisualChild<T>(v);
  69. }
  70. if (child != null)
  71. {
  72. break;
  73. }
  74. }
  75. return child;
  76. }
  77. }

c# WPF DataGrid 获取选中单元格信息的更多相关文章

  1. WPF DataGrid 获取选中 一行 或者 多行

    WPF中DataGrid使用时,需要将其SelectedItem转换成DataRowView进行操作 然而SelectedItem 与SelectedItems DataGrid的SelectionU ...

  2. 【转】WPF DataGrid 获取选中的当前行某列值

    方法一:DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; string result = mySelectedE ...

  3. WPF DataGrid 获取选中的当前行某列值

    方法一: DataRowView mySelectedElement = (DataRowView)dataGrid1.SelectedItem; ]ToString(); 方法二: var a = ...

  4. WPF:获取DataGrid控件单元格DataGridCell

    转载:http://blog.csdn.net/jhqin/article/details/7645357 /* ------------------------------------------- ...

  5. WPF备忘录(3)如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

  6. 如何从 Datagrid 中获得单元格的内容与 使用值转换器进行绑定数据的转换IValueConverter

    一.如何从 Datagrid 中获得单元格的内容 DataGrid 属于一种 ItemsControl, 因此,它有 Items 属性并且用ItemContainer 封装它的 items. 但是,W ...

  7. WinForm中DataGridView复制选中单元格内容解决方案

    WinForm中DataGridView鼠标选中单元格内容复制方案 1.CTR+C快捷键复制 前提:该控件ClipboardCopyMode属性设置值非Disable: 2.鼠标框选,自定义代码实现复 ...

  8. jqgrid cellEdit为true的时候,默认选中单元格值的解决方案

    jqgrid cellEdit为true的时候,点击单元格的时候,鼠标在单元格最前面闪. 这时候如果要修改数字内容,非常麻烦.要全选单元格内容,不然不好改. 点击单元格的时候,默认选中单元格值的解决方 ...

  9. GridView获取单个单元格的值

    0.GridView中的所有数据都存储在Rows集合中,可以通过Rows的Cell属性获取单个单元格的值:如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,从单元格检索控件:如果 ...

随机推荐

  1. Css3-文字

    一.text-overflow   text-overflow用来设置是否使用一个省略标记(...)标示对象内文本的溢出. 语法:text-overflow:clip(默认属性,表示剪切) | ell ...

  2. ES6——函数-箭头函数

    箭头函数: 1.普通函数 function 函数名(){...} 2.箭头函数 注意:  1)如果只有一个返回值,{}return可以省略: let arr = [12,5,8,99,33,14,26 ...

  3. es5和es6中的this指向问题

    const test ={ id:2, a:function(){ var a_this=this; setTimeout(function(){ console.log('a:',this,a_th ...

  4. ViewMode

    一.ViewMode 实现使用场景-Model枚举的情景下, 注意:枚举声明在后台的时候,需要渲染界面,页面表格使用 Bootstrap Table插件-事先通过ajax 渲染(数据库读取值1.2.3 ...

  5. qt如何去掉文件路径最后一个反斜杠的内容

    QString strTemp = “E:\\hell\\word\\detect.txt”   int nIndex = strTemp.lastIndexOf('\\');    strTemp ...

  6. java ArrayList存储基本类型

    package java06; /* 如果希望像集合ArrayList中存储基本数据类型数据,必须使用基本数据类型对应的“包装类” 基本数据类型 包装类(引用类型,包装类都位于java.lang包下 ...

  7. 【串线篇】数据库设计之加谈n-n

    // n-n即(1-n)+(n-1): // 1-n:n-1:n-n:外键应该放在哪个表? //结论: //一对n:外键一定放在n的一端:别让一个人去记14亿,而让大家只记一个习大大 //n-n:”中 ...

  8. 51nod 1605:棋盘问题

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1605 题目中最有用的点其实还是x必为奇数 #include& ...

  9. 英语单词forwarding

    forwarding 来源——xshell的远程连接 [c:\~]$ Connecting to ... Connection established. To escape to local shel ...

  10. java通过url调用远程接口返回json数据

    java通过url调用远程接口返回json数据,有用户名和密码验证, 转自 https://blog.csdn.net/wanglong1990421/article/details/78815856 ...