WPF:獲取DataGrid控件單元格DataGridCell


  • 轉載: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  
  •     {  
  •         /// <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;  
  •         }  
  •     }  
  • }  

  • 免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM