WPF - 在样式中使用ControlTemplate资源

Jam*_*add 10 wpf xaml styles controltemplate

创建样式时,是否可以将ControlTemplate属性设置为先前定义的资源?例如,如果我在ResourceDictionary中有以下内容:

<ControlTemplate x:Key="MyControlTemplate" TargetType="{x:Type Button}">
...
</ControlTemplate>
Run Code Online (Sandbox Code Playgroud)

后来想在这样的样式中使用它:

<Style x:Key="MyStyle" TargetType="{x:Type Button}">
    <Setter Property="Template" Value="???"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

那可能吗?

And*_*erd 12

我相信这会奏效:

<Style x:Key="MyStyle" TargetType="{x:Type Button}">    
    <Setter Property="Template" Value="{StaticResource MyControlTemplate}"/>
</Style>
Run Code Online (Sandbox Code Playgroud)

  • 重要的是,必须在样式之前定义模板.否则它是_XamlParseException:找不到具有Name/Key的资源YourTemplateKey_ (11认同)