在WPF中獲取DataGridTemplateColumn模板定義的內(nèi)容控件
(轉(zhuǎn)載)
xaml格式描述:
<DataGrid Name="dataGrid" Grid.Row="1" ItemsSource="{Binding}" > <DataGrid.Columns> <DataGridTemplateColumn Header="描述"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Expander x:Name="expander" Header="{Binding Describe}"> <TextBlock Text="{Binding Path=Exception}" TextWrapping="Wrap" MinHeight="30" MinWidth="250" /> </Expander> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
現(xiàn)在要獲取expander控件,代碼如下:
int index = dataGrid.CurrentCell.Column.DisplayIndex;
DataGridTemplateColumn templeColumn = dataGrid.Columns[index] as DataGridTemplateColumn;
if(templeColumn == null) return;
object item = dataGrid.CurrentCell.Item;
FrameworkElement element = templeColumn.GetCellContent(item); Expander expander= templeColumn.CellTemplate.FindName("expander", element);
|