获取WPF树结构中的控件

本文介绍了一个用于简化 DataGrid 控件使用的 C# 辅助类。该类提供了查找特定单元格内控件的方法,支持按名称搜索 Visual 类型的子元素,并能够获取指定行列的 DataGridCell 对象。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class DataGridHelper 
    
        private DataGrid dataGrid; 
        public DataGridHelper(DataGrid dataGrid) 
        
            this.dataGrid = dataGrid; 
        
     
        public T FindCellControl<T>(string name, int columnIndex) where T : Visual 
        
            DataRowView selectItem = dataGrid.SelectedItem as DataRowView; 
            DataGridCell cell = GetCell(dataGrid, dataGrid.SelectedIndex, columnIndex); 
     
            return FindVisualChildByName<T>(cell, name) as T; 
        
     
        public T FindVisualChildByName<T>(Visual parent, string name) where T : Visual 
        
            if (parent != null
            
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) 
                
                    var child = VisualTreeHelper.GetChild(parent, i) as Visual; 
                    string controlName = child.GetValue(Control.NameProperty) as string
                    if (controlName == name) 
                    
                        return child as T; 
                    
                    else
                    
                        T result = FindVisualChildByName<T>(child, name); 
                        if (result != null
                            return result; 
                    
                
            
            return null
        
     
        public static DataGridCell GetCell(DataGrid datagrid, int rowIndex, int columnIndex) 
        
            DataGridRow rowContainer = GetRow(datagrid, 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
        
     
        public static DataGridRow GetRow(DataGrid datagrid, int columnIndex) 
        
            DataGridRow row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex); 
            if (row == null
            
                datagrid.UpdateLayout(); 
                datagrid.ScrollIntoView(datagrid.Items[columnIndex]); 
                row = (DataGridRow)datagrid.ItemContainerGenerator.ContainerFromIndex(columnIndex); 
            
            return row; 
        
     
        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; 
        
    
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值