Chapter 9. Properties

The preceding chapters in this book include close to 100 examples that demonstrate all sorts of control properties, methods, events, and other features provided by WPF. To keep those examples as simple and self-contained as possible, I sometimes glossed over some techniques used in the code and deferred them to later chapters.

This chapter covers one of those topics: properties. Many properties are relatively straightforward, and XAML, C#, and Visual Basic code can easily work with them.

Others, however, are more confusing. Some properties don't take values that don't have simple data types such as Integer or String, and using them can be confusing, particularly in XAML code.

This chapter describes WPF properties. It explains how you can set even complicated property values in XAML, C#, and Visual Basic code.

Property Basics

The idea of a property is fairly simple: a property is a value that belongs to an object and that determines its behavior, appearance, or the data that it represents.

For example, if you create a Person class, it probably has properties like FirstName, LastName, and StreetAddress that define the data that a Person object represents.

Using this kind of simple property is easy. For example, the following C# code creates a Person object and sets its FirstName and LastName properties:

// Create a new Person object.
Person author = new Person();

// Set the Person's name properties.
author.FirstName = "Rod";
author.LastName = "Stephens";

XAML code also ...

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.