1

How do I access an element contained in a DataTemplate that is displayed through a ContentControl. I have a ContentControl which hosts a PresentationModel along the lines of:

<ContentControl x:Name="ContentContainer"
                Content="{Binding}" 
                ContentTemplate="{Binding ContentControlTemplate, ElementName=this}"

Where "this" is the view (UserControl).

There's a DataGridControl I want to EndEdit on, so I tried this:

ContentPresenter presenter = VisualTreeHelper.GetChild(this. ContentContainer, 0) as ContentPresenter;
DataGridControl dg = this. ContentContainer.ContentTemplate.FindName("datagrid", presenter) as DataGridControl;
dg.EndEdit();

Problem is that the ContentControl has no children, maybe because of the way the content is bound?

I appreciate any help.

4

1 回答 1

0

好吧,您正在将 GetChild 的结果投射到 ContentPresenter。根据其模板,情况可能并非如此。我认为它的默认模板包括一个边框,所以你的演员将返回 null。如果除了显示内容不需要它做任何事情,为什么不直接使用 ContentPresenter 呢?

于 2009-08-07T20:35:28.780 回答