我有一个数据网格,其中包含一些定义的列,然后是行详细信息模板。如何在隐藏代码中访问行详细信息模板中的控件?我有一个按钮,我想以编程方式启用/禁用,但我不知道如何在后面的代码中访问它。我在MSDN上看到过这个:
http://msdn.microsoft.com/en-us/library/bb613579.aspx 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);
KeywordsGrid
是与我相关的变量DataGrid
。在我的电话中注意到FindVisualChild
,我正在使用DataGridDetailsPresenter
类而不是ContentPresenter
(这是关键......它迫使FindVisualChild
方法一路迭代所有内容呈现器,直到找到行详细信息)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)