Styles and Control Templates - Reusing Styles
(Page 3 of 4 )
In addition to saving you from typing out the name of the class prefix for every property name, the TargetTypeattribute will also check that all classes that have the style applied are an instance of that type (or derived type). What that means is that if we leaveTargetTypeset toControl, we can apply it to aButtonelement, but not to aTextBlockelement, as the former derives ultimately fromControl but the latter does not.
On the other hand, whileControlandTextBlockboth share the common ancestorFrameworkElement, theFrameworkElementclass doesn’t define aFontSizedependency property, so a style with aTargetTypeofFrameworkElementwon’t let us set theFontSizeproperty because its not there, despite the fact that bothControlandTextBlockhave aFontSizeproperty.
Even with theTargetTypeset toControl, we gain a measure of reuse of our style across classes that derive fromControl—e.g.,Button,Label,Window, etc. However, if we drop theTargetTypeattribute from the style altogether, we gain a measure of reuse of styles across controls that don’t have a common base but share a dependency-property implementation. In my experimentation, I’ve found that dependency properties that share the same name across classes, such asControl.FontSizeandTextBlock.FontSize, also share an implementation. What that means is that even thoughControlandTextBlockeach define their ownFontSizeproperty, at runtime they share the implementation of this property, so I can write code like Example 5-8.
Example 5-8. Reusing a style between different element types
...
<Style x:Key="CellTextStyle">
<Setter Property="Control.FontSize" Value="32" />
</Style>
...
<!-- derives from Control -->
<Button Style="{StaticResource CellTextStyle}" ... />
<!-- does *not* derive from Control -->
<TextBlock Style="{StaticResource CellTextStyle}" ... />
...
In Example 5-8, I’ve dropped theTargetType attribute from the style definition, using instead the class prefix on each property the style sets. This style can be applied to aButtonelement, as you’d expect, but can also be applied to aTextBlock control, with theFontSizeset as specified by the style. The reason this works is that both theButton, which gets itsFontSizedependency property definition from the Control class, and theTextBlock, which provides it’s ownFontSizedependency property definition, share theFontSizedependency property implementation with theTextElementclass. Figure 5-2 shows the relationship of elements to their dependency-property implementations.
As Figure 5-2 shows, if we wanted to, we could redefine our style in terms of theTextElementclass, even though it falls into the inheritance tree of neitherControlnorTextBlock, as in Example 5-9.

Figure 5-2. Elements and dependency properties
Example 5-9. Depending on the implementation of dependency properties
<Style x:Key="CellTextStyle">
<Setter Property="TextElement.FontSize" Value="32" />
</Style>
...
<Button Style="{StaticResource CellTextStyle}" ... />
<TextBlock Style="{StaticResource CellTextStyle}" ... />
Taking this further, if we’d like to define a style that contains properties not shared by every element we apply them to, we can do that, too, as in Example 5-10.
Example 5-10. Styles can have properties that targets don’t have
<Style x:Key="CellTextStyle">
<Setter Property="TextElement.FontSize" Value="32" />
<Setter Property="Button.IsCancel" Value="False" />
</Style>
...
<!-- has an IsCancel property --> <Button Style="{StaticResource CellTextStyle}" ... />
<!-- does *not* have an IsCancel property -->
<TextBlock Style="{StaticResource CellTextStyle}" ... />
In Example 5-10, we’ve added theButton.IsCancelproperty to theCellTextStyle and applied it to theButtonelement, which has this property, and theTextBlockelement, which doesn’t. This is OK. At runtime, WPF will apply the dependency properties that exist on the elements that have them and silently swallow the ones that aren’t present.
Next: Named Styles >>
More XML Articles
More By O'Reilly Media
|
This article is excerpted from chapter five of the book Programming Windows Presentation Foundation, written by Chris Sells and Ian Griffiths (O'Reilly; ISBN: 0596101139). Check it out today at your favorite bookstore. Buy this book now.
|
|