Skip to end of metadata
Go to start of metadata

Styling and templating refer to a suite of features (styles, templates, and storyboards) that allow developers and designers to create visually compelling effects and to create a consistent appearance for their product.

Although developers and or designers can customize the appearance extensively on an application-by-application basis, a strong styling and templating model is necessary to allow maintenance and sharing of the appearance within and among applications.

With this styling model there are clear separation of presentation and logic. This means that designers can work on the appearance of an application by using only XAML at the same time that developers work on the programming logic by using C# or Visual Basic.

This overview focuses on the styling and templating aspects of the application and does not discuss any data binding concepts. For more information about data binding, see Data Binding Overview.

Style Basics

You can think of a Style as a convenient way to apply a set of property values to more than one element. For example, consider the following GlassButton elements and their default appearance:


You can change the default appearance by setting properties, such as FontSize, Width, Padding, Margin on each GlassButton element directly. However, if you want your GlassButton elements to share some properties, you can create a Style in the Resources section of your XAML file, as shown here:


When you set the TargetType of your style to the GlassButton type, the style is applied to all the GlassButton elements in the user control.

Now the GlassButton elements appear as follows:

Extending Styles

Perhaps you want your two GlassButton elements to share some property values, such as the FontSize and Width, but you also want the "Next" Button to have some additional properties. You can do that by creating a new style that is based on the first style, as shown here:


Notice that the previous style is given an x:Key. To apply the style, you set the Style property on your GlassButton to the x:Key value, as shown here:


The GlassButton now has different Foreground but share the same other properties as the other button.

The following shows what the GlassButton elements now look like:

Relationship of the TargetType Property and the x:Key Attribute

As shown in the first example, setting the TargetType property to GlassButton without assigning the style an x:Key causes the style to be applied to all GlassButton elements. In this case, the x:Key is implicitly set to {x:Type GlassButton}. This means that if you explicitly set the x:Key value to anything other than {x:Type GlassButton}, the Style is not applied to all GlassButton elements automatically. Instead, you must apply the style (by using the x:Key value) to the GlassButton elements explicitly. If your style is in the resources section and you do not set the TargetType property on your style, then you must provide an x:Key.

In addition to providing a default value for the x:Key, the TargetType property specifies the type to which setter properties apply. If you do not specify a TargetType, you must qualify the properties in your Setter objects with a class name by using the syntax Property="ClassName.Property". For example, instead of setting Property="FontSize", you must set Property to "GlassButton.FontSize" or "Control.FontSize".

Also note that many ClientUI controls consist of a combination of other ClientUI controls. If you create a style that applies to all controls of a type, you might get unexpected results. For example, if you create a style that targets the StylishLabel type in your application, the style is applied to all StylishLabel controls in the your application, even if the StylishLabel is part of another control, such as a UXListBoxItem.

Styles and Resources

You can use a style on any element that derives from FrameworkElement or. The most common way to declare a style is as a resource in the Resources section in a XAML file, as shown in the previous examples. Because styles are resources, they obey the same scoping rules that apply to all resources; where you declare a style affects where the style can be applied. For example, if you declare the style in the root element of your application definition XAML file, the style can be used anywhere in your application. If you create a navigation application and declare the style in one of the application's XAML files, the style can be used only in that XAML file.

Setting Styles Programmatically

To assign a named style to an element programmatically, get the style from the resources collection and assign it to the element's Style property. Note that the items in a resources collection are of type Object. Therefore, you must cast the retrieved style to a Style before assigning it to the Style property. For example, to set the defined Extended style on a GlassButton named nextButton, do the following:


Customizing Visual States

Most of ClientUI controls has visual states embedded to provides nice user experience. These visual states can also be customized by modifying the Control Template.

The following shows GlassButton's control template which contains some visual states.


As you can see, there are some visual states defined in the control templates such as MouseOver, Pressed, Checked, Normal, etc. Each of this visual states contain storyboard that you can modify so that when the visual state is triggered it will use the modified settings.

ItemsControl Styles and Data Templates

For controls that have collection of items the styling consist of two parts, styling the parent control and styling the items collection. Styling the parent control is the same as styling any other control as discusses in previous section, while styling the items collection requires different approach.

Each ItemsControl type has a corresponding item container type. These item container types indicate the control type used as the item of each ItemsControl that you can easily customize using ItemsContainerStyle. To learn more about item container types, see ItemsControl Overview.

Styling using ItemContainerStyle

Styling using ItemsContainerStyle property is the basic way to style the items of ItemsControl. As discussed above each ItemsControl has an item container type, so basically you just need to provide the style of the item container type and assign it to ItemsContainerStyle property.

The following example shows how to style the item of UXListBox using ItemsContainerStyle property.


Styling using ItemContainerStyleSelector

To cover advanced styling such as creating alternating style between items or highlighting certain items based on certain scenario ClientUI provides ItemContaierStyleSelector property at ItemsControl level.

This property requires an object that inherit from StyleSelector class that will do the custom logic to determine which style will be applied to the specific item.

The following example shows how to use StyleSelector to create alternate item style in UXListBox.


Templating using ItemTemplate

When the ItemsControl is bound to a collection of object, you can use ItemTemplate to control the look and feel of the content.

The following example shows how to perform templating using ItemTemplate property.


Templating using ItemTemplateSelector

Similiar with ItemContaierStyleSelector, you can also create a template selector by creating a class that inherit from DataTemplateSelector. This template selector allows you to apply different template on different item based on the logic describes in the template selector.

The following example shows how to use DataTemplateSelector to create alternate item template in UXListBox.