Content Control + DataTemplate dynamically changing UserControl DevExpress wpf

2.1k views Asked by At

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

There are 1 answers

1
toadflakz On BEST ANSWER

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