Chapter 13. Styles and Property Triggers

Chapter 12 explains resources — named property values that you can assign to controls to give them a common appearance and behavior.

Unfortunately, resources often lead to long and complicated code. If a group of controls shares many property values, then converting the properties into resources requires a lot of repetition using the verbose StaticResource or DynamicResource keywords.

A Style is a special kind of resource that lets you extract this redundancy and centralize it, much as other resources let you centralize property values. Styles let you define packages of property values that you can apply to a control all at once instead of applying individual resource values to the control one at a time.

This chapter explains Styles and shows how to use them to give controls similar appearance and behavior.

Simplifying Properties

Resources make it easier to give a group of controls a consistent appearance. The ButtonValues example program shown in Figure 13-1 displays five button-like rectangles containing text. It sets each button's properties individually, so making changes to the code is repetitive and a bit tricky. If you don't change every button's code in the same way, they will no longer have a common appearance.

Figure 13-1

Figure 13.1. Figure 13-1

The ButtonResources example program uses resources to display a similar set of buttons. The resources make ...

Get WPF Programmer's Reference: Windows Presentation Foundation with C# 2010 and .NET 4 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.