C# 如何在视图模型中绑定StaticResource键

C# 如何在视图模型中绑定StaticResource键,c#,.net,wpf,xaml,binding,C#,.net,Wpf,Xaml,Binding,对于一个画布,我有不同的ControlTemplates: <Application.Resources> <ControlTemplate x:Key="Control1" /> <ControlTemplate x:Key="Control2" /> </Application.Resources> 最后在我看来使用它: <UserControl Template="{StaticResource {Binding Te

对于一个画布,我有不同的ControlTemplates:

<Application.Resources>
    <ControlTemplate x:Key="Control1" />
    <ControlTemplate x:Key="Control2" />
</Application.Resources>
最后在我看来使用它:

<UserControl Template="{StaticResource {Binding Template}}" />

但它不起作用,我怎么能修复它?
谢谢

您可以尝试使用DataTriggers

<UserControl>
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Setter Property="ControlTemplate" value="{StaticResource Control1}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Template}" Value ="Control1">
<Setter Property="ControlTemplate" value="{StaticResource Control1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Template}" Value ="Control2">
<Setter Property="ControlTemplate" value="{StaticResource Control2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
</UserControl>

<UserControl>
<UserControl.Style>
<Style TargetType="{x:Type UserControl}">
<Setter Property="ControlTemplate" value="{StaticResource Control1}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Template}" Value ="Control1">
<Setter Property="ControlTemplate" value="{StaticResource Control1}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Template}" Value ="Control2">
<Setter Property="ControlTemplate" value="{StaticResource Control2}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Style>
</UserControl>