在我的用户的资源节中的代码:WPF MVVM绑定ContentConrol的的ContentTemplate的静态资源
<UserControl.Resources>
<DataTemplate x:Key="BillingLevel" DataType="{x:Type local:ViewBillingLevel}">
<local:ViewBillingLevel Width="Auto" Height="Auto"/>
</DataTemplate>
<DataTemplate x:Key="Suburb" DataType="{x:Type local:ViewSuburb}">
<local:ViewSuburb Width="Auto" Height="Auto" />
</DataTemplate>
</UserControl.Resources>
我想做的结合部分:
<ContentControl Grid.Row="2" ContentTemplate="{StaticResource Suburb}" Margin="5" />
喜欢的东西:
<ContentControl Grid.Row="2" ContentTemplate="{StaticResource={Binding myDataTemplateKey}}" Margin="5" />
所以我可以在后面的C#代码中的DataTemplates之间切换。 我不知道这是否可能,或者如何以另一种方式实现。
谢谢。
是否要切换相同内容类型或其他内容的数据类型?我们是在谈论2-3个不同的模板还是关于*许多*?为了根据某些值管理少量模板,我可能会编写一个基于DataTrigger的答案,但这对于具有许多不同模板的更通用的上下文来说并不有用。 – grek40