100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
.NET

Styles and Setters

Learn how the Style element and Setter objects apply consistent property values across controls, including BasedOn inheritance and triggers.

Resources and StylingBeginner9 min readJul 10, 2026
Analogies

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.

xml
<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

Was this page helpful?

Topics covered

#NET#XAMLStudyNotes#MicrosoftTechnologies#StylesAndSetters#Styles#Setters#Anatomy#Style#StudyNotes#SkillVeris