Pro WPF 4.5 in C# by Matthew MacDonald

Pro WPF 4.5 in C# by Matthew MacDonald

Author:Matthew MacDonald [MacDonald, Matthew]
Language: eng
Format: epub
Tags: Programming, Microsoft Programming, C#
Publisher: Apress®
Published: 2011-12-05T05:00:00+00:00


Figure 18-4. A color picker custom control with two different templates

Documenting Template Parts

There’s one last refinement that you should make to the previous example. Good design guidelines suggest that you add the TemplatePart attribute to your control declaration to document what part names you use in your template and what type of control you use for each part. Technically, this step isn’t required, but it’s a piece of documentation that can help others who are using your class (and it can also be inspected by design tools that let you build customized control templates, such as Expression Blend).

Here are the TemplatePart attributes you should add to the ColorPicker control class:

[TemplatePart(Name="PART_RedSlider", Type=typeof(RangeBase))]

[TemplatePart(Name = "PART_BlueSlider", Type=typeof(RangeBase))]

[TemplatePart(Name="PART_GreenSlider", Type=typeof(RangeBase))]

public class ColorPicker : System.Windows.Controls.Control

{ ... }

FINDING A CONTROL’S DEFAULT STYLE

Every control has a default style. You call DefaultStyleKeyProperty.OverrideMetadata() in the static constructor of your control class to indicate what default style your custom control should use. If you don’t, your control will simply use the default style that’s defined for the control that your class derives from.

Contrary to what you might expect, the default theme style is not exposed through the Style property. All the controls in the WPF library return a null reference for their Style property.

Instead, the Style property is reserved for an application style (the type you learned to build in Chapter 11). If you set an application style, it’s merged into the default theme style. If you set an application style that conflicts with the default style, the application style wins and overrides the property setter or trigger in the default style. However, the details you don’t override remain. This is the behavior you want. It allows you to create an application style that changes just a few properties (for example, the text font in a button), without removing the other essential details that are supplied in the default theme style (such as the control template).

Incidentally, you can retrieve the default style programmatically. To do so, you can use the FindResource() method to search up the resource hierarchy for a style that has the right element-type key. For example, if you want to find the default style that’s applied to the Button class, you can use this code statement:

Style style = Application.Current.FindResource(typeof(Button));



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.