473,804 Members | 2,132 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 SpecialCustomBu tton : 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.ge neric.xaml, I define the template:
<ControlTemplat e x:Key="template CustomButton"
TargetType="{x: Type local:CustomBut ton}}">
<!-- Definitions Here -->
</ControlTemplate >

<Style TargetType="{x: Type local:CustomBut ton}">
<Setter Property="Templ ate"
Value="{StaticR esource templateCustomB utton}" />
</Style>

I try to reference the same control template in
SpecialCustomBu tton.generic.xa ml
<Style TargetType="{x: Type local:SpecialCu stomButton}">
<Setter Property="Templ ate"
Value="{Dynamic Resource templateCustomB utton}" />
</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 3738
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 "templateCustom Button" control template resource in a
separate resource dictionary, and merge it into the
"SpecialCustomB utton.generic.x aml" resource dictionary, so that you could
reference it as it is with local resources.

Second option: Instead of using string ResourceKey to identify
"templateCustom Button" control template, try strongly typed
ComponentResour ceKey, something like the following:

<ControlTemplat e x:Key="{Compone ntResourceKey
ResourceId=temp lateCustomButto n, TypeInTargetAss embly={x:Type
local:CustomBut ton}}">
<TextBlock Text="SpecialCu stomButton"/>
</ControlTemplate >

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

<Style TargetType="{x: Type local:SpecialCu stomButton}">
<Setter Property="Templ ate" Value="{Dynamic Resource {ComponentResou rceKey
ResourceId=temp lateCustomButto n, TypeInTargetAss embly={x:Type
local:CustomBut ton}}}" />
</Style>

--------------------------------------------------
Best regards,
Macro Zhou (v-*****@online.mi crosoft.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****@microsof t.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
21823
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 and if so, how? Here's how I'm creating my thumb so far. cornerThumb = new Thumb(); // Set some arbitrary visual characteristics. cornerThumb.Cursor = customizedCursor; cornerThumb.Height = cornerThumb.Width = 10;
6
7495
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 imagine that when i'll succeed this aim, my understanding will be much better than now) is to create a personalized textbox,
3
4815
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 of the Popup to also match the parent control. It cannot use the Transparent background of the ComboBox, because it may span outside the parent control. However, we want there to be enough flexibility to place this on different controls, so I...
1
2572
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. After doing some research, I found that this is a WPF layout problem. When we place a control into a parent control, if the child control's Width and Height properties are not set, the child control will be streched to
8
11527
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 file. each control has a border with another style, and each border has a unique path inside of it. I need to dynamically add these content controls using c# at runtime and am having trouble referencing the styles and adding the path into the...
1
1581
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 creating, it would be nice to use a DispatchTimer to "ramp up" a value, depending on how long the mouse is held down. Thanks,
3
1278
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 don't say is... WHY WPF? What did we get by having a (almost) parallel design palette. If the features were so good, why not just fold them into the parallel controls in VB?
7
10920
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: <Expander Style="{DynamicResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:CustomResources}, ResourceId=CustomExpanderStyle1}}" >
2
5224
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 is a brain freeze, but what could I possibly be missing? WtS
0
9715
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9595
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10603
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10099
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7643
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.