Styling Controls

Programmers like to tinker with the look of controls. There are two ways to do so in Silverlight: minor adjustments can be made with styles, and wholesale redesigns with templates.

To illustrate how to use styles, we'll start with the program you just wrote. You can just make a copy, but the safest way is to create a new project and copy the XAML and the classes into the new namespace. Here are the steps:

  1. Create a new project (let's call it BookStyles).

  2. In the original Page.xaml, collapse the Grid as shown in Figure 7-18 and copy it.

    Collapse and copy the Grid

    Figure 7-18. Collapse and copy the Grid

  3. In the new Page.xaml, collapse the Grid and paste the old Grid over it.

  4. Back in the original Page.xaml.cs, collapse and copy the Page class (as Figure 7-19 shows) and paste it into the new project. Then copy and paste the using statements.

    Collapse and copy the Page class

    Figure 7-19. Collapse and copy the Page class

  5. In the new project, create a Book.cs file and then collapse and copy the Book class from the old project to the new one. Then copy and paste the using statements.

  6. Run the new project to ensure that everything is working properly.

Applying Styles Inline

Let's start by adding some inline styling to the TextBlock for the "Title" prompt:

<TextBlock x:Name="TitlePrompt" Text="Title: " VerticalAlignment="Bottom" HorizontalAlignment="Right" ...

Get Programming .NET 3.5 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.