Content generated from ContentTemplate does not have DataContext of Silverlight ContentControl set
Asked Answered
H

2

6

In my Silverlight 4 application, I have a ContentControl with its ContentTemplate property bound to a property in the data context. That works fine. However, the content of the template once rendered has its DataContext set to null. I would like the content to inherit the same DataContext as set for the ContentControl. Is there a way to get this to happen?

Hit answered 13/7, 2010 at 18:46 Comment(0)
H
-1

I found an alternate way to accomplish what was required. In my case, the template (not the content template) of the ContentControl was unimportant, so I made my DataTemplate objects into ControlTemplate objects instead and bound the Template property of the ContentControl instead of ContentTemplate. The data context was preserved if I did it this way.

Hit answered 13/7, 2010 at 19:24 Comment(1)
+1 Question, -1 Answer; I came here wanting to know the same thing, but didn't find an answer. The accepted answer works around the question, but doesn't answer it.Ifc
Z
10

The ContentControl's template has the ContentControl's Content property as a DataContext. So try

<ContentControl Content="{Binding}" />

if this is merely the current DataContext.

Zelmazelten answered 30/7, 2011 at 15:55 Comment(0)
H
-1

I found an alternate way to accomplish what was required. In my case, the template (not the content template) of the ContentControl was unimportant, so I made my DataTemplate objects into ControlTemplate objects instead and bound the Template property of the ContentControl instead of ContentTemplate. The data context was preserved if I did it this way.

Hit answered 13/7, 2010 at 19:24 Comment(1)
+1 Question, -1 Answer; I came here wanting to know the same thing, but didn't find an answer. The accepted answer works around the question, but doesn't answer it.Ifc

© 2022 - 2024 — McMap. All rights reserved.