码迷,mamicode.com
首页 > 其他好文 > 详细

遍历WPF DataGrid单元格

时间:2014-06-23 07:31:36      阅读:1016      评论:0      收藏:0      [点我收藏+]

标签:class   com   get      name   for   

  • using System.Windows.Controls; 
  • using System.Windows.Controls.Primitives; 
  • using System.Windows.Media; 
  •  
  • namespace Splash.WPF 
  •     public static class DataGridPlus 
  •     { 
  •         /// <summary> 
  •         /// 获取DataGrid控件单元格 
  •         /// </summary> 
  •         /// <param name="dataGrid">DataGrid控件</param> 
  •         /// <param name="rowIndex">单元格所在的行号</param> 
  •         /// <param name="columnIndex">单元格所在的列号</param> 
  •         /// <returns>指定的单元格</returns> 
  •         public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)         
  •         { 
  •             DataGridRow rowContainer = dataGrid.GetRow(rowIndex); 
  •             if (rowContainer != null) 
  •             { 
  •                 DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 
  •                 DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); 
  •                 if (cell == null) 
  •                 { 
  •                     dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[columnIndex]); 
  •                     cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(columnIndex); 
  •                 } 
  •                 return cell; 
  •             } 
  •             return null; 
  •         } 
  •  
  •         /// <summary> 
  •         /// 获取DataGrid的行 
  •         /// </summary> 
  •         /// <param name="dataGrid">DataGrid控件</param> 
  •         /// <param name="rowIndex">DataGrid行号</param> 
  •         /// <returns>指定的行号</returns> 
  •         public static DataGridRow GetRow(this DataGrid dataGrid, int rowIndex)         
  •         { 
  •             DataGridRow rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex); 
  •             if (rowContainer == null) 
  •             { 
  •                 dataGrid.UpdateLayout(); 
  •                 dataGrid.ScrollIntoView(dataGrid.Items[rowIndex]); 
  •                 rowContainer = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex); 
  •             } 
  •             return rowContainer; 
  •         } 
  •  
  •         /// <summary> 
  •         /// 获取父可视对象中第一个指定类型的子可视对象 
  •         /// </summary> 
  •         /// <typeparam name="T">可视对象类型</typeparam> 
  •         /// <param name="parent">父可视对象</param> 
  •         /// <returns>第一个指定类型的子可视对象</returns> 
  •         public static T GetVisualChild<T>(Visual parent) where T : Visual 
  •         { 
  •             T child = default(T); 
  •             int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
  •             for (int i = 0; i < numVisuals; i++) 
  •             { 
  •                 Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
  •                 child = v as T; 
  •                 if (child == null) 
  •                 { 
  •                     child = GetVisualChild<T>(v); 
  •                 } 
  •                 if (child != null) 
  •                 { 
  •                     break; 
  •                 } 
  •             } 
  •             return child; 
  •         } 
  •     } 

遍历WPF DataGrid单元格,布布扣,bubuko.com

遍历WPF DataGrid单元格

标签:class   com   get      name   for   

原文地址:http://www.cnblogs.com/HelloXZ/p/3799582.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!