ControlTemplates, Styles, and Custom UserControls


What this chapter covers:
  • Understanding ControlTemplates

  • How Styles work with ControlTemplates

  • Creating a Button ControlTemplate

  • Creating Styles and using them to overwrite WPF controls

  • Using Styles to mandate how controls display their content

  • Using ResourceDictionaries

  • Applying resources to WPF controls

  • Using Styles and ControlTemplates to create your own custom WPF UserControls

  • How to create and use custom UserControls

A very important concept in object-oriented programming is one that says you can create something once and then reuse it over and over again. Of course, this means you can reuse a resource in one application, but in WPF you can take this further and reuse a resource over and over again in other applications. ControlTemplates and UserControls allow you to do just that. I have been working with WPF now for well over a year, and in that time I have been able to create my own libraries of resources that I am now able to reuse in new applications that I create. This allows me to develop new applications faster because I don’t have to re-create new resources, but rather add a reference to them in my new applications and then simply make use of them. In this chapter, you are going to create a Style for a WPF Button control that will contain a ControlTemplate, and then change that ControlTemplate to have the Button show an Image control. You are then going to create a ResourceDictionary and move the Button Style to it. Finally, you are going to take everything you have learned and create a custom UserControl.


Visual Studio Image Control Content Property Default Behavior Property Panel 
These keywords were added by machine and not by the authors. This process is experimental and the keywords may be updated as the learning algorithm improves.


Unable to display preview. Download preview PDF.

Unable to display preview. Download preview PDF.

Copyright information

© Victor Gaudioso 2008

Personalised recommendations