当我尝试指定多个DataTemplates供ContentControl使用,以便使用正确的模板(基于Type),结果得到的Content只是Content的ToString()值。
<ContentControl DataContext="{Binding MyTreeRootViewModels}" Content="{Binding /, Path=CurrentlySelectedTreeViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="x:Type vm:TypeAViewModel">
<StackPanel>
<local:TypeAUserControl />
</StackPanel>
</DataTemplate>
<DataTemplate DataType="x:Type vm:TypeBViewModel">
<StackPanel>
<local:TypeBUserControl />
</StackPanel>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
在上面的示例中,当TypeAViewModel
的树节点由CurrentlySelectedTreeViewModel
返回时,我会看到显示MyApp.ViewModel.TypeAViewModel
。 我希望能看到我自己的TypeAViewModelUserControl
。
我尝试在我的某个数据模板中放置一个单独的<TextBlock Text="TESTING"/>
元素,只是为了查看问题是否与我的用户控件有关。 结果相同。
有任何想法我做错了什么吗?
(顺便说一下,CurrentlySelectedTreeViewModel
是返回我TreeView
中当前选定节点的属性。它似乎工作得很好-当我在树中选择节点时,该节点的正确类型名称出现在ContentControl
中)。
DataTemplates
放在ContentControl.Resources
中。你的例子就是我的解决方案! :) - mbmcavoy