Anatomy of a Style
A Style is a XAML object that bundles a set of property assignments meant to be applied consistently to one or more controls of a given TargetType. Inside a Style, each Setter element names a Property (a dependency property on the target type, such as Background or FontSize) and a Value, and when the style is applied to a control, every Setter's value is assigned to that property unless the control has already set that property locally, since local values always take precedence over style values.
Cricket analogy: It is like a fielding coach issuing a standard positioning instruction for all part-time spinners, such as 'set a deep square leg by default,' which every bowler follows unless the captain gives that specific bowler a different field for that over.
<Style x:Key="PrimaryButtonStyle" TargetType="Button">
<Setter Property="Background" Value="#2563EB" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="14" />
<Setter Property="Padding" Value="14,8" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#1D4ED8" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Opacity" Value="0.5" />
</Trigger>
</Style.Triggers>
</Style>Setter Property and Value
Setter.Property is resolved against dependency properties registered on the TargetType, so a Style targeting Button can set Button-specific properties as well as inherited ones from Control and FrameworkElement, and it can equally target attached properties such as Grid.Column by writing Property="Grid.Column". A Setter.Value can be a simple literal, a markup extension like {StaticResource AccentBrush}, or a complex inline object such as a nested Gradient definition, giving styles the same expressive range as setting the property directly on an element.
Cricket analogy: It is like a bowling coach's instruction sheet that can set a bowler's run-up length directly, or reference a shared 'standard fast-bowler run-up' template, or even spell out a fully custom multi-step approach for one bowler.
Style Inheritance with BasedOn
The BasedOn attribute lets one Style extend another Style that targets the same type or a base type, inheriting all of its setters and triggers, and then adding new setters or overriding specific ones without repeating the entire definition. This is the primary mechanism for building a style hierarchy, such as a base ButtonStyle that every button style in the app derives from, with DangerButtonStyle and SuccessButtonStyle each overriding only the Background setter.
Cricket analogy: It is like a fast-bowling academy's advanced program being based on the beginner program, inheriting all the fundamental drills and adding extra pace-specific sessions on top rather than rebuilding the curriculum from zero.
Setters aren't limited to normal properties: writing Property="Grid.Row" or Property="Canvas.Left" inside a Style targeting the right element lets you set attached properties consistently across many elements placed in a Grid or Canvas.
Triggers Inside Styles
Style.Triggers holds Trigger and MultiTrigger elements that conditionally apply extra setters when a property on the styled control matches a given value, such as darkening a Button's background when IsMouseOver becomes true, without writing any code-behind or event handlers. MultiTrigger extends this to require several conditions simultaneously, such as IsMouseOver being true AND IsEnabled being true, before its nested setters take effect, which is useful for compound visual states like a hovered-and-enabled look that must not apply while the control is disabled.
Cricket analogy: It is like an umpire's automatic signal rule: a boundary is signaled the instant the ball crosses the rope, no manual review needed, while a no-ball-and-six combined signal only fires when both the front-foot fault and the six condition are true together, similar to a MultiTrigger.
Setters inside a Style.Triggers block only take effect while the trigger condition is true and are automatically reverted when the condition becomes false, but if multiple triggers set the same property, the last matching trigger in document order wins, which can create confusing visual results if triggers aren't ordered deliberately.
- A Style bundles Setter elements that assign property values to controls of a matching TargetType.
- Local property values on a control always override values from a Style's setters.
- Setter.Property can target both normal dependency properties and attached properties.
- Setter.Value accepts literals, markup extensions, or complex inline objects.
- BasedOn lets one Style inherit and extend another style's setters and triggers.
- Style.Triggers applies conditional setters based on property state without code-behind.
- MultiTrigger requires multiple conditions to be true simultaneously before its setters apply.
Practice what you learned
1. What takes precedence when a control has both a locally set property value and a Style setter for that same property?
2. Which attribute lets one Style inherit the setters and triggers of another Style?
3. Can a Setter's Property attribute target an attached property like Grid.Column?
4. What distinguishes MultiTrigger from a regular Trigger inside Style.Triggers?
5. If two triggers in the same Style.Triggers block both set the Background property and both conditions are currently true, which value is applied?
Was this page helpful?
You May Also Like
Implicit vs Explicit Styles
Understand the difference between explicit styles applied with x:Key and implicit styles that auto-apply by TargetType, and when to use each.
Resource Dictionaries in XAML
Learn how ResourceDictionary stores reusable brushes, styles, and templates in XAML, and how StaticResource and DynamicResource look them up.
Theming with XAML
Learn how to structure XAML resource dictionaries to support light/dark theming, runtime theme switching, and consistent theme resource naming.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows 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 TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics
Microsoft TechnologiesMVVM Design Pattern Study Notes
.NET · 30 topics