0
votes

I want to dynamically load user control to a content control,so i used the "Good Old Method" of changing usercontrol based on DataTemplate.

But on setting the ViewModel property i.e binded to ContentControl content property, ContentControl is displaying "text" conatining the respective viewmodel`s name , instead of loading the respective Usercontrol.

Window`s Resources

 <dxr:DXRibbonWindow.Resources>
    <DataTemplate x:Key="TYRVM"  DataType="{x:Type VM:ProductTYRViewModel}">
        <views:TYRUserControl/>
    </DataTemplate>
    <DataTemplate x:Key="THORVM" DataType="{x:Type VM:ProductTHORViewModel}">
        <views:THORUserControl/>
    </DataTemplate>
 <dxr:DXRibbonWindow.Resources>

Window`s code

  <ContentControl Name="content" Content="{Binding Path=VMs,Mode=TwoWay}"/>

Now i don`t know what exactly the problem is!!

1
try ContentControl.Resources with your DataTemplates to check wether they are working. if they do you can go on and check whats happen to your resources in DXRibbonWindow.blindmeis
Maybe your binding (VMs) is not good. What output says? Maybe data context is not set, or maybe contentcontrol has wrong datacontext. You should probably give us your more detailed xaml codeIgor
@Igor I have tried checking the ContentControl binding, values are getting updated.But the problem is instead of displaying the usercontrol,ContentControl is displaying "text" i.e viewmodel name.Abhinav Sharma
And VMs is indeed instance of VM:ProductTYRViewModel or VM:ProductTHORViewModel? I made small example, mimicking your code and everything works correctly. I know this is probably stupid, but maybe views:THORUserControl or views:TYRUserControl have something like <TextBlock Text="{Binding}"/> (maybe you did that for testing and forgot about that)?Igor

1 Answers

1
votes

You have specified a Key on the DataTemplate so the DataTemplate is not automatically applied based on the Type.

This from MSDN:

This property that is very similar to the TargetType property of the Style class. When you set this property to the data type without specifying an x:Key, the DataTemplate gets applied automatically to data objects of that type. Note that when you do that the x:Key is set implicitly. Therefore, if you assign this DataTemplate an x:Key value, you are overriding the implicit x:Key and the DataTemplate would not be applied automatically.

Link to MSDN : DataTemplate.DataType