在WPF中获取DataGridTemplateColumn模板定义的内容控件

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>

 

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

int index = dataGrid.CurrentCell.Column.DisplayIndex;

 

 

转载:http://blog.sina.com.cn/s/blog_541e699d0100wjs9.html

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);

posted @ 2014-10-30 13:26  玖彩技术团队  阅读(1451)  评论(0编辑  收藏  举报
以上只是个人想法和实践经验,如果有文字错误和语法错误,请加以指点! QQ:247039968 emil:wujc@younger.com 无论是美女的歌声,还是鬣狗的狂吠,无论是鳄鱼的眼泪,还是恶狼的嚎叫,都不会使我动摇