473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

WPF: ControlTemplate of Inherited Custom Control

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
Aug 7 '08 #1
1 3714
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 (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:
ms****@microsoft.com.

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

Aug 8 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: moondaddy | last post by:
I'm writing a windows app in WPF and want to change the shape of a thumb to an ellipse. Is this possible? also, the edges of the thumb are beveled. Is it possible to change this to a flat look...
6
by: Vivien Parlat | last post by:
Hello, I hope i'm posting into the good group, i found no active group around xaml. My situation is the following: I'm trying to play around xaml in the new VC# Express 2008. My "aim" (i...
3
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I am creating a ControlTemplate for a ComboBox. This ComboBox will have a transparent background so it uses the control it is placed in as its background. I would like the background...
1
by: Linda Liu[MSFT] | last post by:
Hi Moondaddy, I downloaded your sample project and run it on my machine. I did see the problem on my side. The image drawn in the Button is not as clear as that one drawn in the Image control. ...
8
by: moondaddy | last post by:
I'm posting code for a user control ( FunctionConnectorSelector) below which has 3 content controls in it. each content control uses a style from a resource dictionary merged into the app.xaml...
1
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello All-- Just a general (or perhaps loaded) question - is it considered good or bad practice to use code-behind with a CustomControl.generic.xaml file? For instance, with a control I'm...
3
by: Just_a_fan | last post by:
I have been through two books where they talk about the (almost) parallel control set for WPF and VB. They go through the small difference and talk about everything going on a form but what they...
7
by: Linda Liu[MSFT] | last post by:
Hi George, I have downloaded your sample solution and built it on my machine. I got a compilation error indicating that the type of "CustomResources" doesn't exist in the following xaml code: ...
2
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have an existing (WPF) Class Library project that I'd like to add a WPF Custom Control to. However, when I try to Add New Item, that isn't one of the available options. Perhaps this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.