472,954 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 3684
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.