WPF: Get the DataGrid control cell DataGridCell

Source: Internet
Author: User
Tags wpf datagrid

  1. Reprint: http://blog.csdn.net/jhqin/article/details/7645357
  2. /* ----------------------------------------------------------
  3. File name: DataGridPlus.cs
  4. Qin Jianhui
  5. Msn:[email protected]
  6. qq:36748897
  7. Blog: http://blog.csdn.net/jhqin
  8. Development environment:
  9. Visual Studio V2010
  10. . NET Framework 4 Client profile
  11. Version history:
  12. V1.0 June 07, 2012
  13. WPF DataGrid Control Extension method
  14. Resources:
  15. http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b7299e55-92e2-4a6b-8987-869fef8f22eb/
  16. ------------------------------------------------------------ */
  17. Using System.Windows.Controls;
  18. Using System.Windows.Controls.Primitives;
  19. Using System.Windows.Media;
  20. Namespace SPLASH.WPF
  21. {
  22. public static class Datagridplus
  23. {
  24. // <summary>
  25. /// Get the DataGrid control cell
  26. // </summary>
  27. /// <param name= "DataGrid" >datagrid controls </param>
  28. /// <param name= "RowIndex" > Cell line number </param>
  29. /// <param name= "ColumnIndex" > the column number where the cell is located </param>
  30. /// <returns> designated cells </returns>
  31. public static DataGridCell Getcell (thisdatagrid datagrid, int rowIndex, int columnindex)
  32. {
  33. DataGridRow Rowcontainer = Datagrid.getrow (RowIndex);
  34. if (rowcontainer! = null)
  35. {
  36. Datagridcellspresenter presenter = getvisualchild<datagridcellspresenter> (Rowcontainer);
  37. DataGridCell cell = (DataGridCell) presenter. Itemcontainergenerator.containerfromindex (columnindex);
  38. if (cell = = null)
  39. {
  40. Datagrid.scrollintoview (Rowcontainer, Datagrid.columns[columnindex]);
  41. Cell = (DataGridCell) presenter. Itemcontainergenerator.containerfromindex (columnindex);
  42. }
  43. return cell;
  44. }
  45. return null;
  46. }
  47. // <summary>
  48. /// get the row for the DataGrid
  49. // </summary>
  50. /// <param name= "DataGrid" >datagrid controls </param>
  51. /// <param name= "RowIndex" >datagrid line number </param>
  52. /// <returns> specified line number </returns>
  53. public static DataGridRow GetRow (thisdatagrid datagrid, int rowIndex)
  54. {
  55. DataGridRow Rowcontainer = (datagridrow) dataGrid.ItemContainerGenerator.ContainerFromIndex (RowIndex);
  56. if (Rowcontainer = = null)
  57. {
  58. Datagrid.updatelayout ();
  59. Datagrid.scrollintoview (Datagrid.items[rowindex]);
  60. Rowcontainer = (datagridrow) dataGrid.ItemContainerGenerator.ContainerFromIndex (RowIndex);
  61. }
  62. return rowcontainer;
  63. }
  64. // <summary>
  65. /// Gets a child visual of the first specified type in the parent visual object
  66. // </summary>
  67. // <typeparam name= "T" > Visual object Type </typeparam>
  68. /// <param name= "Parent" > Parents visual </param>
  69. /// <returns> First child visual object of the specified type </returns>
  70. public static T getvisualchild<t> (Visual parent) where T:visual
  71. {
  72. T child = default (t);
  73. int numvisuals = Visualtreehelper.getchildrencount (parent);
  74. For (int i = 0; i < numvisuals; i++)
  75. {
  76. Visual V = (visual) visualtreehelper.getchild (parent, I);
  77. Child = V as T;
  78. if (child = = null)
  79. {
  80. Child = getvisualchild<t> (v);
  81. }
  82. if (child! = null)
  83. {
  84. Break ;
  85. }
  86. }
  87. return child;
  88. }
  89. }
  90. }

WPF: Get the DataGrid control cell DataGridCell

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.