1

I have a user control that I would like to use in two different contexts. The user control needs its DataContext set to an instance of an appropriate ViewModel that has been created by the parent view/viewmodel.

I was hoping for something like:

<local:Child DataContext="{Binding ChildViewModel}"/>

where ChildViewModel is a inpc-styled property of the ViewModel that the page is bound to.

That doesn't seem to work. Is it possible to assign the DataContext by using Binding?

Ralph Shillington
  • 20,718
  • 23
  • 91
  • 154
  • Could you provide entries from Output Window? Basically, all wpf binding exceptions are shown there, so if you have one, we will check it – madcyree Feb 14 '11 at 16:36

1 Answers1

1

It would probably be simpler to bind the Content of an ContentControl to your child ViewModel like this:

<ContentControl Content="{Binding ChildViewModel}" />

..and then have a DataTemplate to apply your local:Child View, like this

<DataTemplate DataType="{x:Type local:ChildViewModel}">
    <local:Child />
</DataTemplate>
Barracoder
  • 3,696
  • 2
  • 28
  • 31