Files
docs-desktop/dotnet-desktop-guide/framework/wpf/controls/guidelines-for-designing-stylable-controls.md
T
Andy (Steve) De George e9fcf43083 Merge pull request #1135 from dotnet/adegeo-links
Correct .NET Framework links that pointed to old .NET 5 URLs
2021-08-11 12:04:34 -07:00

14 KiB

title, ms.date, helpviewer_keywords, ms.assetid
title ms.date helpviewer_keywords ms.assetid
Guidelines for Designing Stylable Controls 03/30/2017
style design for controls [WPF]
controls [WPF], style design
c52dde45-a311-4531-af4c-853371c4d5f4

Guidelines for Designing Stylable Controls

This document summarizes a set of best practices to consider when designing a control which you intend to be easily stylable and templatable. We came to this set of best practices through a lot of trial and error while working on the theme control styles for the built-in [!INCLUDETLA2#tla_winclient] control set. We learned that successful styling is as much a function of a well-designed object model as it is of the style itself. The intended audience for this document is the control author, not the style author.

Terminology

"Styling and templating" refer to the suite of technologies that enable a control author to defer the visual aspects of the control to the style and template of the control. This suite of technologies includes:

  • Styles (including property setters, triggers, and storyboards).

  • Resources.

  • Control templates.

  • Data templates.

For an introduction to styling and templating, see Styling and Templating.

Before You Start: Understanding Your Control

Before you jump into these guidelines, it is important to understand and have defined the common usage of your control. Styling exposes an often unruly set of possibilities. Controls that are written to be used broadly (in many applications, by many developers) face the challenge that styling can be used to make far-reaching changes to the visual appearance of the control. In fact, the styled control may not even resemble the control author's intentions. Since the flexibility offered by styling is essentially boundless, you can use the idea of common usage to help you scope your decisions.

To understand your control's common usage, it's good to think about the value proposition of the control. What does your control bring to the table that no other control can offer? Common usage does not imply any specific visual appearance, but rather the philosophy of the control and a reasonable set of expectations about its usage. This understanding allows you to make some assumptions about the composition model and the style-defined behaviors of the control in the common case. In the case of xref:System.Windows.Controls.ComboBox, for example, understanding the common usage won't give you any insight about whether a particular xref:System.Windows.Controls.ComboBox has rounded corners, but it will give you insight into the fact that the xref:System.Windows.Controls.ComboBox probably needs a pop-up window and some way of toggling whether it is open.

General Guidelines

Theme Considerations

  • Theme styles should attempt to have consistent property semantics across all themes, but make no guarantee. As part of its documentation, your control should have a document describing the control's property semantics, that is, the "meaning" of a property for a control. For example, the xref:System.Windows.Controls.ComboBox control should define the meaning of the xref:System.Windows.Controls.Control.Background%2A property within xref:System.Windows.Controls.ComboBox. The default styles for your control should attempt to follow the semantics defined in that document across all themes. Control users, on the other hand, should be aware that property semantics can change from theme to theme. In certain cases, a given property may not be expressible under the visual constraints required by a particular theme. (The Classic theme, for example, does not have a single border to which Thickness can be applied for many controls.)

  • Theme styles do not need to have consistent trigger semantics across all themes. The behavior exposed by a control style through triggers or animations may vary from theme to theme. Control users should be aware that a control will not necessarily employ the same mechanism to achieve a particular behavior across all themes. One theme, for example, may use an animation to express hover behavior where another theme uses a trigger. This can result in inconsistencies in behavior preservation on customized controls. (Changing the background property, for example, might not affect the hover state of the control if that state is expressed using a trigger. However, if the hover state is implemented using an animation, changing to background could irreparably break the animation and therefore the state transition.)

  • Theme styles do not need to have consistent "layout" semantics across all themes. For example, the default style does not need to guarantee that a control will occupy the same amount of size in all themes or guarantee that a control will have the same content margins / padding across all themes.

See also