WPF - Use a ControlTemplate resource within a Style

14,172

I believe this will work:

<Style x:Key="MyStyle" TargetType="{x:Type Button}">    
    <Setter Property="Template" Value="{StaticResource MyControlTemplate}"/>
</Style>
Share:
14,172

Related videos on Youtube

James Cadd
Author by

James Cadd

Updated on August 16, 2020

Comments

  • James Cadd
    James Cadd almost 4 years

    When creating a Style, is it possible to set the ControlTemplate property to a previously defined resource? For example, if I had the following in a ResourceDictionary:

    <ControlTemplate x:Key="MyControlTemplate" TargetType="{x:Type Button}">
    ...
    </ControlTemplate>
    

    And then later wanted to use it in a Style like this:

    <Style x:Key="MyStyle" TargetType="{x:Type Button}">
        <Setter Property="Template" Value="???"/>
    </Style>
    

    Is that possible?

  • Agent_L
    Agent_L over 10 years
    The important part is that the template must be defined BEFORE the style. Otherwise it's XamlParseException: Cannot find a Resource with the Name/Key YourTemplateKey
  • N. Kudryavtsev
    N. Kudryavtsev over 5 years
    @Agent_L or one can just use DynamicResource