tags:

views:

107

answers:

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.

A: 

Well, you are casting the result of GetChild to a ContentPresenter. Depending on its Template, this may not be the case. I think its default template includes a Border, so your cast will return null. If you don't need it to do anything other than display the content, why not use ContentPresenter directly?

Abe Heidebrecht
The statement "VisualTreeHelper.GetChild(this. ContentContainer, 0)" returns null, it has no children for some reason, even before the cast.I didn't think to try the Contentpresenter directly, I'll give that a go.Thanks