Skip to content

Guidance for Style/Setter initialization vs. default value in Dependency Property registration #4093

Open
@michael-hawker

Description

@michael-hawker

It is common in XAML to set default values in Style Setters:

        <Setter Property="Padding" Value="{ThemeResource TextControlThemePadding}" />
        <Setter Property="Foreground" Value="{ThemeResource TextControlForeground}" />
        <Setter Property="Background" Value="{ThemeResource TextControlBackground}" />
        <Setter Property="BorderBrush" Value="{ThemeResource TextControlBorderBrush}" />

When writing a new custom dependency property, you can define the default value in code-behind as described by the current document, but you can also add it to the style for a templated control.

There should be a section here that describes the pros/cons of each approach, how they interact with one another, and when to use one approach over the other.

I believe there's ramifications on if you want to use them in Visual State Managers or other things like Template Binding, but I don't remember and not sure, was hoping the documentation would help remind me here.

If this is in some other XAML doc, then it should be referenced here, but this seemed like a good spot to talk about it and branch out elsewhere.


Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions