Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Use a ControlTemplate resource within a Style

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?

like image 649
James Cadd Avatar asked Oct 30 '09 21:10

James Cadd


People also ask

Where do you put ControlTemplate?

The most common way to declare a ControlTemplate is as a resource in the Resources section in a XAML file. Because templates are resources, they obey the same scoping rules that apply to all resources.

What is ControlTemplate WPF?

Platform: WPF Category: Styles and Templates. The ControlTemplate specifies the appearance of a Control. If a Control does not have a ControlTemplate, the Control will not appear in your application.

Where to define style in WPF?

The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.

What is WPF Templatebinding?

A template binding is a special type of data binding that allows you to reference the parent control, read its properties and apply their values. In some cases, you can use the values directly. In other situations you may need to apply value converters or additional formatting.


1 Answers

I believe this will work:

<Style x:Key="MyStyle" TargetType="{x:Type Button}">    
    <Setter Property="Template" Value="{StaticResource MyControlTemplate}"/>
</Style>
like image 176
Andrew Shepherd Avatar answered Sep 18 '22 14:09

Andrew Shepherd