WPF学习笔记(4):获取DataGridTemplateColumn模板定义的内容控件

在之前的DataGrid的DataGridTemplateColumn列中,自定义了一个TextBox控件,但是在C#代码中提示找不到这个控件,导致无法对该控件进行操作。在网上搜索后,发现一些处理方法比较繁琐,下面这个方法最简便。

xaml格式描述:

 1 "dataGrid" Grid.Row="1" ItemsSource="{Binding}"  >
 2        
 3             "描述">
 4                     
 5                         
 6                             
 7                                 
 8                             
 9                         
10                     
11               
12        
13 
 
 

现在要获取expander控件,代码如下:

1 int index = dataGrid.CurrentCell.Column.DisplayIndex;
2 DataGridTemplateColumn templeColumn = dataGrid.Columns[index] as DataGridTemplateColumn;
3 
4 if(templeColumn == null) return;
5 
6 object item = dataGrid.CurrentCell.Item;
7 FrameworkElement element = templeColumn.GetCellContent(item);
8 Expander expander= templeColumn.CellTemplate.FindName("expander", element);

 

原贴地址:https://www.cnblogs.com/eric_ibm/p/3772516.html

原作者:烟灰缸

你可能感兴趣的:(WPF学习笔记(4):获取DataGridTemplateColumn模板定义的内容控件)