Bri*_*anP 9 wpf datagrid rowdetails
我有一个带有一些已定义列的数据网格,然后是一个行详细信息模板.如何在后面的代码中访问行详细信息模板中的控件?我有一个按钮,我想以编程方式启用/禁用,但我无法弄清楚如何在后面的代码中访问它.我在MSDN上看过这个:
http://msdn.microsoft.com/en-us/library/bb613579.aspx
但这只是描述一个常规的数据模板,所以当我尝试它时它不起作用.我的情况是行详细信息数据模板.当然有人编写代码来访问数据网格行详细信息模板中的控件,该模板可以对此进行评论(非常感谢).
好的,我想出了如何使这个工作我不得不调整原始问题中MSDN文章中发布的代码....
DataGridRow row = (DataGridRow)(KeywordsGrid.ItemContainerGenerator.ContainerFromItem(KeywordsGrid.SelectedItem));
// Getting the ContentPresenter of the row details
DataGridDetailsPresenter presenter = FindVisualChild<DataGridDetailsPresenter>(row);
// Finding Remove button from the DataTemplate that is set on that ContentPresenter
DataTemplate template = presenter.ContentTemplate;
Button button = (Button)template.FindName("RemoveItemButton", presenter);
Run Code Online (Sandbox Code Playgroud)
"KeywordsGrid"是绑定到我的datagrid的变量.请注意,在我调用FindVisualChild时,我使用的是"DataGridDetailsPresenter"类,而不是"ContentPresenter"(这是关键...它强制FindVisualChild方法一直遍历所有内容提供者,直到我到达一个为行细节).