Chapter 8. Styles

In a word-processing document, a style is a set of properties to be applied to ranges of content (e.g., text, images, etc.). For example, the name of the style I’m using now is called Normal,Body,b and for this document in prepublication, that means a font family of Times, a size of 10, and full justification. Later in the document, I’ll be using a style called Code,x,s, which will use a font family of Courier New, a size of 9, and left justification. Styles are applied to content to produce a certain look when the content is rendered.

In WPF, a style is also a set of properties applied to content used for visual rendering, like setting the font weight of a Button control. In addition to the features in word-processing styles, WPF styles have specific features for building applications, including the ability to apply different visual effects based on user events. All of these features come without the need to build a custom control (although that’s still a useful thing to be able to do, as discussed in Chapter 18).

Without Styles

As an example of how styles can make themselves useful in WPF, let’s look at a simple implementation of tic-tac-toe (see Example 8-1).

Example 8-1. A simple tic-tac-toe layout
<!-- Window1.xaml --> <Window x:Class="TicTacToe.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="TicTacToe" Height="300" Width="300"> <!-- the black background lets the tic-tac-toe ...

Get Programming WPF, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.