1 /* ----------------------------------------------------------2 file name: DataGridPlus.cs3 4 Qin Jianhui5 6 Msn:[email protected]7 qq:367488978 9 Blog:Http://blog.csdn.net/jhqinTen One Development Environment: A Visual Studio V2010 - . NET Framework 4 Client profile - the Version history: - V1.0 June 07, 2012 - WPF DataGrid Control extension Method - + References: - http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b7299e55-92e2-4a6b-8987-869fef8f22eb/ + ------------------------------------------------------------ */ A usingSystem.Windows.Controls; at usingSystem.Windows.Controls.Primitives; - usingSystem.Windows.Media; - - namespaceSPLASH.WPF - { - Public Static classDatagridplus in { - /// <summary> to ///get the DataGrid control cell + /// </summary> - /// <param name= "DataGrid" >DataGrid Control</param> the /// <param name= "RowIndex" >the line number where the cell is located</param> * /// <param name= "ColumnIndex" >the column number where the cell is located</param> $ /// <returns>the specified cell</returns>Panax Notoginseng Public StaticDataGridCell Getcell ( ThisDataGrid DataGrid,intRowIndex,intcolumnindex) - { theDataGridRow Rowcontainer =Datagrid.getrow (rowIndex); + if(Rowcontainer! =NULL) A { theDatagridcellspresenter 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); - } the returncell; - }Wuyi return NULL; the } - Wu /// <summary> - ///get row of DataGrid About /// </summary> $ /// <param name= "DataGrid" >DataGrid Control</param> - /// <param name= "RowIndex" >DataGrid Row Number</param> - /// <returns>the specified line number</returns> - Public StaticDataGridRow GetRow ( ThisDataGrid DataGrid,intRowIndex) A { +DataGridRow Rowcontainer =(DataGridRow) DataGrid.ItemContainerGenerator.ContainerFromIndex (rowIndex); the if(Rowcontainer = =NULL) - { $ datagrid.updatelayout (); the Datagrid.scrollintoview (Datagrid.items[rowindex]); theRowcontainer =(DataGridRow) DataGrid.ItemContainerGenerator.ContainerFromIndex (rowIndex); the } the returnRowcontainer; - } in the /// <summary> the ///gets a child visual of the first specified type in the parent visual object About /// </summary> the /// <typeparam name= "T" >visual Object Type</typeparam> the /// <param name= "parent" >Parent Visual Object</param> the /// <returns>the first child visual object of the specified type</returns> + Public StaticT getvisualchild<t> (Visual parent)wheret:visual - { theT Child =default(T);Bayi intNumvisuals =Visualtreehelper.getchildrencount (parent); the for(inti =0; i < numvisuals; i++) the { -Visual v =(Visual) visualtreehelper.getchild (parent, i); -Child = V asT; the if(Child = =NULL) the { theChild = getvisualchild<t>(v); the } - if(Child! =NULL) the { the Break; the }94 } the returnChild ; the } the }98}
The DataGrid gets datagridrow and DataGridColumn