Styling a control that supports templates (Blend for Visual Studio)
Blend for Visual Studio 2012 makes it very easy to customize controls by creating and modifying control templates and styles. This results in a unique and consistent look for your application.
Templates and styles define the pieces that make up a control and the default behavior of the control, respectively. You create templates and styles by making copies of the default system styles and templates for a control (because you can't modify system styles and templates). Modifying templates and styles is an easy way to essentially make new controls in Design view of Blend, without having to use code.
The difference between styles and templates
The following table provides a detailed comparison of styles and templates to help you decide whether you want to modify the style or template of a control, or both:
Styles |
Templates |
|
---|---|---|
Purpose |
By using styles, you can modify the default values of properties that are set on the control to which the style is applied. For example, you can specify default colors for the background, border, and foreground of a control such as a button. These style properties can be overridden by the values that are set on the control itself when it is drawn on the artboard. For example, if you set the background color to blue in the style of a button, the button will appear blue when it is drawn on the artboard, but you can change the color. You can set only pre-existing properties in the style. For example, you cannot set a default value for a property that belongs to a new part that you added to the template. Finally, you can use styles to specify the default behavior of a control. For example, in a style for a button, you can create an event handler so that when a user moves the pointer over the button, the background color will change. These property changes are instantaneous (they cannot be animated gradually). |
Using templates, you can modify the structure of the control to which the template is applied. You can modify a control template to rearrange, add, or delete the objects (or parts) in the control. For example, you can add a background image or design to a control such as a button. You can also modify the values of properties (such as background color) that are set on the control to which the template is applied. These template values cannot be overridden by the values that are set on the control itself when it is drawn on the artboard. However, you can use template-binding to set the properties of a template according to the values of properties of the control when it is drawn on the artboard. When you modify a template, you have access to more parts of a control than when you modify a style. For example, you can change the way the pop-up list appears in a combo box, or you change the look of the button that triggers the pop-up list in the combo box by modifying the items template. Some templates consist of the following parts:
Finally, you can specify the behavior of any new and existing parts in a template by using code. For example, you can create an event handler so that when a user moves the pointer over a button, the color of one of the parts will change. These property changes can be instantaneous or animated to produce a smooth transition.
Note
You cannot animate from the value of a template-bound property or a color resource to another value, use specific property values.
|
How to edit |
You can enter the editing mode for a style in the following ways: Using the menu
The Edit Style option will not be available if the system style is applied to the control. For an example, see Create a style (Blend for Visual Studio). Using the Resources panel (to modify an existing style)
Note
Styles and templates are resources that you can apply to controls in different ways and store in different locations in your application. For an example of creating a style, see Apply a style resource (Blend for Visual Studio).
|
You can enter the editing mode for a control template in the following ways: Using the menu
Using the Resources panel (to modify an existing template)
Tip
Control templates are wrapped inside styles so that the style that is applied to a control includes both the appearance (parts) and the behavior for the control. You click Edit Current instead of Edit a Copy because the copy of the style includes the template. To return to the editing scope of your main document, you must click Return scope to two times because the first click brings you back to the editing mode of the style.
Note
Styles and templates are resources that you can apply to controls in different ways and store in different locations in your application. For an example of creating a template, see Create or modify a template (Blend for Visual Studio).
|
How to apply |
You can apply an existing style to a control on the artboard in the following ways: Using the menu
Using the Assets panel to draw a styled control on the artboard
|
You can apply an existing template to a control on the artboard in the following ways: Using the menu
Using the Assets panel to draw a templated control on the artboard Templates are contained in styles. To apply a template to a control on the artboard, use the same method as for applying styles. |
Note
Blend includes a set of standard styles packaged in a resource dictionary in Windows Store projects. This file (StandardStyles.xaml) is located in the Projects panel in the Common folder.
Modifying styles and templates
When you are in the editing scope of a style or template in Blend, a breadcrumb navigation bar appears at the upper left of the artboard.
Breadcrumb bar with template-editing mode selected
By clicking the buttons on the breadcrumb bar, you can quickly move through template-editing mode, style-editing mode, and object-editing scope for the selected object. The breadcrumb bar appears for any selected object that has a custom style or template applied to it.
For information about how to modify a style or template, see Edit a style (Blend for Visual Studio) and Create or modify a template (Blend for Visual Studio).