dotnet / dotnet/wpf

Add DefaultStyleKeyAttribute

Open
#4,369 0 comments 1 reaction 0 assignees View on GitHub
Partner Request
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

**Why is this needed:** to better support very popular Copy Template feature in Visual Studio XAML designer.

**Details**: [DefaultStyleKey](https://docs.microsoft.com/en-us/dotnet/api/system.windows.frameworkelement.defaultstylekey) value is available/discoverable during runtime only. To enable Copy Template in XAML intellisense (i.e. without XAML designer or running app) we need a way to find it. Consider following code which is most common way of customizing default style key for user controls
```
public class MyButton : MuButtonBase
{
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
}
```
This is not reflection/Roslyn friendly wrt finding that `typeof(MyButton)` is default style key. This can be addressed by adding DefaultStyleKeyAttribute, e.g.
```
[DefaultStyleKey(typeof(MyButton))]
public class MyButton : MuButtonBase
{
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
}

// To be added to WPF
public class DefaultStyleKeyAttribute : Attribute
{
public DefaultStyleKeyAttribute(Type type) => this.DefaultStyleKey = type;
public Type DefaultStyleKey { get; }
}
```

This will be similar to other "tooling" attributes like [BrowsableAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.browsableattribute) or [CategoryAttribute](https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.categoryattribute).

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.