471,330 Members | 1,090 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

WPF: ControlTemplate of Inherited Custom Control


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

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 -->

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

I try to reference the same control template in
<Style TargetType="{x:Type local:SpecialCustomButton}">
<Setter Property="Template"
Value="{DynamicResource templateCustomButton}" />
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?

Aug 7 '08 #1
1 3576
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
<TextBlock Text="SpecialCustomButton"/>

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}}}" />

Best regards,
Macro Zhou (v-*****@online.microsoft.com, 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:

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

Aug 8 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Vivien Parlat | last post: by
3 posts views Thread by =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post: by
1 post views Thread by Linda Liu[MSFT] | last post: by
1 post views Thread by =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post: by
3 posts views Thread by Just_a_fan | last post: by
2 posts views Thread by =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.