Styles: Centralizing Property Values
A Style bundles a TargetType with a collection of Setter elements, each assigning a property/value pair, letting a developer define a button's Background, Padding, and CornerRadius once and reuse it across many Button instances instead of repeating those attributes everywhere. Styles can be implicit — declared with no x:Key so they apply automatically to every matching TargetType in scope — or explicit, referenced by setting an element's Style property to a {StaticResource} lookup by x:Key, and BasedOn lets one Style extend another, inheriting its Setters and overriding just the ones that differ.
Cricket analogy: Like a team's standard kit specification — navy blazer, specific badge — applied to every player by default, with a captain's variant simply adding an armband on top of that base style (BasedOn).
Resource Dictionaries and Theme Resources
Styles, Brushes, and ControlTemplates are typically organized into ResourceDictionary files and combined via ResourceDictionary.MergedDictionaries, most commonly merged together at the Application level in App.xaml so every page in the app can reference them. For colors and brushes that must respond to the user's light, dark, or high-contrast theme, use {ThemeResource} instead of {StaticResource}: a ThemeResource binding re-resolves automatically whenever the active theme changes at runtime, while a StaticResource binding resolves once and never updates.
Cricket analogy: Like a cricket board's central rulebook shared across every stadium, with certain rules — such as when floodlights and the pink ball are used — automatically adjusted for day matches versus day-night Tests, similar to how ThemeResource adapts to light versus dark theme.
<Page.Resources>
<Style x:Key="PrimaryButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{ThemeResource SystemAccentColor}"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="Padding" Value="16,8"/>
<Setter Property="CornerRadius" Value="4"/>
</Style>
<Style x:Key="EmphasisButtonStyle" TargetType="Button"
BasedOn="{StaticResource PrimaryButtonStyle}">
<Setter Property="FontWeight" Value="Bold"/>
</Style>
</Page.Resources>
<Button Content="Save" Style="{StaticResource EmphasisButtonStyle}"/>Prefer ThemeResource over StaticResource for brushes and colors that should respond to the user's light/dark theme setting or high-contrast mode. StaticResource resolves once and never updates if the theme changes at runtime, while ThemeResource re-resolves automatically.
Control Templates: Redefining Visual Structure
While a Style only sets existing properties, a ControlTemplate replaces a control's entire visual tree — swapping out the default Button chrome for a completely custom shape, for example — while the control's underlying behavior and public API (Click events, Command binding, IsEnabled) stay intact. Inside a ControlTemplate, TemplateBinding connects an element back to the templated control's own property, such as binding a custom border's Background to TemplateBinding Background so the template still respects whatever the consumer set on the control.
Cricket analogy: Like completely redesigning a stadium's seating and pitch layout, the visual structure, while keeping the same match rules and scoring behavior, the control's underlying logic, intact.
VisualStateManager Inside Templates
A default control's ControlTemplate almost always defines VisualStateManager.VisualStateGroups covering interaction states such as Normal, PointerOver, Pressed, and Disabled, each with Setters or a Storyboard that changes the template's appearance in response to that state — dimming a button when disabled, for instance. When you author a custom ControlTemplate from scratch, none of that comes for free: you must redeclare the equivalent VisualStateGroups yourself, or the control will lose its expected visual feedback for hover, press, and disabled interactions even though the underlying behavior still technically works.
Cricket analogy: Like a fielder's stance visibly changing — relaxed while waiting, alert as the bowler runs in (PointerOver), diving at the moment of the shot (Pressed), and static and roped-off if injured and substituted (Disabled).
When you replace a control's default ControlTemplate, you take over its entire visual tree, including any VisualStateManager states like Disabled, PointerOver, and Focused. Forgetting to reimplement these states means the control can lose expected accessibility and interaction feedback, such as no longer visually dimming when disabled.
- A Style bundles a set of Setter property/value pairs for a given TargetType, keeping visual configuration out of individual element attributes.
- Styles can be implicit (no x:Key, applied automatically to every element of that TargetType in scope) or explicit (referenced via x:Key).
- BasedOn lets one Style inherit and extend another Style's Setters, similar to class inheritance.
- ResourceDictionaries organize shared resources (Styles, Brushes, Templates) and can be merged at the App, Page, or control level via MergedDictionaries.
- ThemeResource re-resolves automatically when the app's light/dark/high-contrast theme changes, unlike StaticResource which resolves once.
- A ControlTemplate completely replaces a control's visual tree via TemplateBinding while preserving its underlying behavior and properties.
- VisualStateManager inside a ControlTemplate defines interaction states like PointerOver, Pressed, and Disabled that must be reimplemented if the default template is replaced.
Practice what you learned
1. What does BasedOn do when applied to a Style?
2. What is the key difference between StaticResource and ThemeResource?
3. What connects a property inside a ControlTemplate back to the templated control's own property value?
4. What must a developer typically reimplement when replacing a control's default ControlTemplate?
5. How does an implicit Style differ from an explicit one?
Was this page helpful?
You May Also Like
XAML in UWP
An introduction to XAML markup for Universal Windows Platform apps, covering syntax, namespaces, and the difference between {Binding} and {x:Bind}.
UWP Controls and Layout
How UWP's layout panels and built-in controls work together, including Grid sizing, RelativePanel, and ListView virtualization.
Adaptive Design in UWP
Techniques for building UWP UI that adapts across phone, tablet, desktop, and Xbox using VisualStateManager, AdaptiveTrigger, and RelativePanel.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics