Reusing Styles

In addition to saving you from typing out the name of the class prefix for every property name, the TargetType attribute will also confirm that all classes that have the style applied are an instance of that type (or derived type). That means that if we leave TargetType set to Control, we can apply it to a Button element, but not to a TextBlock element, as the former derives ultimately from Control but the latter does not.

However, if we'd like to define a style that contains properties not shared by every element to which we'd like to apply them, we can do that by dropping the TargetType and putting back the property prefix, as shown in Example 8-8.

Example 8-8. Styles can have properties that targets don't have

<Style x:Key="CellTextStyle"> <Setter Property="TextElement.FontSize" Value="32pt" /> <Setter Property="Button.IsCancel" Value="False" /> </Style>

<Button Style="{StaticResource CellTextStyle}" ... />

<!-- does *not* have an IsCancel property --> <TextBlock Style="{StaticResource CellTextStyle}" ... />

In Example 8-8, we've added the Button.IsCancel property to the CellTextStyle and applied it to the Button element, which has this property, and the TextBlock element, which doesn't. This is OK. At runtime, WPF will apply the dependency properties and the elements themselves will ignore those values that don't apply to them.*

WPF's ability to apply styles to objects that don't have all of the properties defined in the style is analogous to applying the Word Normal style, which includes a font size property of its own, to both a range of text and an image. Even though Word knows that images don't have a font size, it applies the portions of the Normal style that do make sense (like the justification property), ignoring the rest.

Getting back to our sample, we can use the CellTextStyle on the Buttons to show nice Xs and Os, and on a TextBlock in a new row to show whose turn it is (see Example 8-9).

* The ability to set a value for a property that an element doesn't have is useful for inheritable properties, because those values will flow on through to child elements. See Chapter 18 for a description of dependency property inheritance.

Example 8-9. Applying a style to Button and TextBlock elements

<Window.Resources> <Style x:Key="CellTextStyle"> <Setter Property="Control.FontSize" Value="32pt" /> <Setter Property="Control.FontWeight" Value="Bold" /> </Style> </Window.Resources> <Grid Background="Black"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions>

<Button Style="{StaticResource CellTextStyle}" ... />


Style="{StaticResource CellTextStyle}" Foreground="White"

Grid.Row="3" Grid.ColumnSpan="3" Name="statusTextBlock" /> </Grid> </Window>

With our new text block in place, we can inform the next player of her turn by updating the CurrentPlayer property setter:

string CurrentPlayer { get { return this.currentPlayer; } set {

this.currentPlayer = value; this.statusTextBlock.Text =

"It's your turn, " + this.currentPlayer;

This reuse of the style across controls of different types gives us a consistent look in the application, as shown in Figure 8-3.

One thing you'll notice is that the status text in Figure 8-3 is white, whereas the text in the buttons is black. Because black is the default text color, if we want the status text to show up against a black background, we have to change the color to something else, hence the need to set the Foreground property to white on the TextBlock. Setting per-instance properties works just fine in combination with a style, and you can combine the two techniques of setting property values as you see fit.

Figure 8-3. A tic-tac-toe game with style

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment