How to easily customize WPF controls – and 5 reasons why you should not do it in a large project

In this article, we are going to explore the process of easily change the look and feel of a WPF control. There are valid scenarios for doing this and there are reasons why it should not be done in a large project.

How to easily customize the control

First things first, we need a project to play with. To do this, create a new WPF project in Visual Studio. To make things easier, be sure that you use the WPF App (.NET Framework) project template. For demonstration purposes, we are going to change the appearance of a button.

Follow the next steps:

  1. If you just created the WPF App project, you will have the MainWindow.xaml open, otherwise add a User Control (WPF) item to your project. If the control that you are using is a WPF control, it does not matter how it is called or what type of WPF control it is for the demonstration.
  2. Drop a button on the control from the Toolbox in the visual designer.
  3. Still in the visual designer, right click on the button and select Edit Template / Edit a Copy… in the popup menu.
  4. For the demonstration purposes, you can accept the default values in the dialog window coming up. Press Ok.

The process we just went through, created a Style in the XAML code. If the name was not taken already in your codebase, this style is likely called ButtonStyle1. The style code here, is basically a copy of how WPF sets up our control, when there are no additional styles attached to it. Any styles we apply to the button is on the top of this style.

To change a bit the look of the button, let’s change the border to purple when the mouse is moved over the control. Should be visually different enough.

  1. Above the Style, insert the following line into the template:

<SolidColorBrush x:Key=”PurpleBorder” Color=”Purple” />

  1. In the Trigger attached to the IsMouseOver property change the BorderBrush property setter to look:

<Setter Property="BorderBrush" TargetName="border"

    Value="{StaticResource PurpleBorder}"/>

  1. Run the application, bring up the control and hover over the button. You will see that instead of the default bluish color, now it has purple border when the mouse is over the control.

When should you customize the control this way

This solution is ideal, when building up mockup applications during a design meeting either with a customer, a potential client or other people involved in the design of the application.

However, you should never consider your mockup as part of the final application. I think we all committed this error during software development several times. Usually, if you do this, sooner or later you will have to pay the price for transforming the customized controls to proper software components, if you want to reuse them.

5 reasons why you should not use this method in a large project

So, why should not you use this method outside of design discussions?

  1. It gets messy quickly.
  2. Using triggers are part of behavior and the way how your control is built up should be the internal matter of your control only.
  3. Does not result in a reusable control, each control must be assigned with the style. This could be fine, until you decide that the control base needs to be changed to add functionality. Now you can go through all the code and try to find who and why changed a behavior partially.
  4. Even if you put the style into a resource dictionary, it is easy to build up style chains that is hard to debug.
  5. Suppose you want to reuse your control in a different assembly. Now you end up with a situation, where you need to copy all resources, including styles and images that are related to your control to the new assembly.

I am sure that we can probably add thousands of more reasons to the list, but these are the top ones. Instead of falling again into the pitfall, stay with me and from next week we will start to build up reusable WPF controls from code.

You can connect with me on LinkedIn or Twitter. Share if you found this article useful or know somebody who would benefit reading about the above topics.

By subscribing to the email list, you will be notified about new posts.
No comments yet.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress. Designed by WooThemes