简体   繁体   中英

Content Control + DataTemplate dynamically changing UserControl DevExpress wpf

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 ie 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!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM