Chapter 17. Creating Custom Controls

Silverlight 3 introduces an extensible control model, which you can leverage to encapsulate logic and common functionality. Custom controls allow you to follow the Silverlight pattern of separation between the presentation and logical functionality of the control. This architecture allows you to reuse functionality, while allowing a designer to completely change out the user interface displayed by the control.

In this chapter, you will discover how to:

  • Create a Silverlight Class Library with a basic control.

  • Reference a custom control from a Silverlight application.

  • Work with the Parts Control Model.

  • Take advantage of visual states.

  • Create a dependency property.

  • Build a content control.

Setting Up a Control Project

Start by creating a new Silverlight Class Library (SCL) in Visual Studio. This project can contain as many custom controls as you wish. For each custom control, create a new class that extends from Control or one of its subclasses.

Creating a Control Class

Silverlight exposes a handful of Control classes that you can extend. Selecting an appropriate class to extend will save you time when implementing the control and make your control easier to use. The following table lists a few of the base classes you can consider:

Class

Common Usage

Control

Provides only the essential functions for a control.

ContentControl

Used by controls that are expected to host content

Panel

Layout controls such as Grid, Canvas, StackPanel

ItemsControl

Used to display lists of ...

Get Silverlight™ 3 Programmer's Reference 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.