WPF:获取DataGrid控件单元格DataGridCell
标签:blog http io os ar for sp 文件 on
- 转载:http://blog.csdn.net/jhqin/article/details/7645357
- /* ----------------------------------------------------------
- 文件名称:DataGridPlus.cs
-
- 作者:秦建辉
-
- MSN:splashcn@msn.com
- QQ:36748897
-
- 博客:http://blog.csdn.net/jhqin
-
- 开发环境:
- Visual Studio V2010
- .NET Framework 4 Client Profile
-
- 版本历史:
- V1.0 2012年06月07日
- WPF DataGrid控件扩展方法
-
- 参考资料:
- http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b7299e55-92e2-4a6b-8987-869fef8f22eb/
- ------------------------------------------------------------ */
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Media;
-
- namespace Splash.WPF
- {
- public static class DataGridPlus
- {
- ///
- /// 获取DataGrid控件单元格
- ///
- /// DataGrid控件
- /// 单元格所在的行号
- /// 单元格所在的列号
- /// 指定的单元格
- public static DataGridCell GetCell(this DataGrid dataGrid, int rowIndex, int columnIndex)
- {
- DataGridRow rowContainer = dataGrid.GetRow(rowIndex);
- if (rowContainer != null)
- {
- DataGridCellsPresenter presenter = GetVisualChild(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;
- }
-
- ///
- /// 获取DataGrid的行
- ///
- /// DataGrid控件
- /// DataGrid行号
- /// 指定的行号
- 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;
- }
-
- ///
- /// 获取父可视对象中第一个指定类型的子可视对象
- ///
- /// 可视对象类型
- /// 父可视对象
- /// 第一个指定类型的子可视对象
- public static T GetVisualChild(Visual parent) where T : Visual
- {
- T child = default(T);
- int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
- for (int i = 0; i
- {
- Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
- child = v as T;
- if (child == null)
- {
- child = GetVisualChild(v);
- }
- if (child != null)
- {
- break;
- }
- }
- return child;
- }
- }
- }
WPF:获取DataGrid控件单元格DataGridCell
标签:blog http io os ar for sp 文件 on
原文地址:http://www.cnblogs.com/qq247039968/p/4066058.html
评论