WPF: ControlTemplate of Inherited Custom Control

W

Wonko the Sane

Hello,

I have a custom control, and want to derive a second control from that
control.

public class CustomButton : Button
{
}

public class SpecialCustomButton : CustomButton
{
}

I'd like to use the same base ControlTemplate for both controls, updating
the template used for the Content in the "special" control. However, I can't
seem to figure out how to "share" the ControlTemplate.

In CustomButton.generic.xaml, I define the template:
<ControlTemplate x:Key="templateCustomButton"
TargetType="{x:Type local:CustomButton}}">
<!-- Definitions Here -->
</ControlTemplate>

<Style TargetType="{x:Type local:CustomButton}">
<Setter Property="Template"
Value="{StaticResource templateCustomButton}" />
</Style>

I try to reference the same control template in
SpecialCustomButton.generic.xaml
<Style TargetType="{x:Type local:SpecialCustomButton}">
<Setter Property="Template"
Value="{DynamicResource templateCustomButton}" />
</Style>


However, the special control never shows up on my panel.

Is there a way to do what I'm attempting to do, or do I have to create a new
ControlTemplate and bind every single property again?

Thanks.
WtS
 
M

Marco Zhou [MSFT]

Hello Wonko,

StaticResource/DynamicResource resource lookup in WPF will be scoped within
the resource dictionary boundaries when you want to use them to refer the
resources within the theme or generic resource dictionaries.

I suppose you define the custom control styles in separate resource
dictionaries, and merge them into generic.xaml resource dictionary. If this
is not the case, please elaborate more one your usage scenario, if this is
the case, there are two options which you could try:

First option: Put the "templateCustomButton" control template resource in a
separate resource dictionary, and merge it into the
"SpecialCustomButton.generic.xaml" resource dictionary, so that you could
reference it as it is with local resources.

Second option: Instead of using string ResourceKey to identify
"templateCustomButton" control template, try strongly typed
ComponentResourceKey, something like the following:

<ControlTemplate x:Key="{ComponentResourceKey
ResourceId=templateCustomButton, TypeInTargetAssembly={x:Type
local:CustomButton}}">
<TextBlock Text="SpecialCustomButton"/>
</ControlTemplate>

And you could use DynamicResource to refer to it as follows:

<Style TargetType="{x:Type local:SpecialCustomButton}">
<Setter Property="Template" Value="{DynamicResource {ComponentResourceKey
ResourceId=templateCustomButton, TypeInTargetAssembly={x:Type
local:CustomButton}}}" />
</Style>

--------------------------------------------------
Best regards,
Macro Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top