microsoft / microsoft/microsoft-ui-xaml

`DependencyProperty.GetMetadata` doesn't handle custom types correctly

Open
#9,939 0 comments 0 reactions 0 assignees View on GitHub
area-CoreFramework bug team-Core team-Markup
Dominant language
C++
Stars
8.4k
Forks
942
Avg merge
2d 7h
Merged PRs (30d)
105

Description

### Describe the bug

Passing a custom type to `DependencyProperty.GetMetadata` doesn't get recognized correctly and the returned metadata can have wrong default value.

### Steps to reproduce the bug

```csharp
public sealed partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
When_IsTabStop();
}

private partial class CustomFE : FrameworkElement { }
private partial class CustomControl : Control { }
private partial class CustomUserControl : UserControl { }

private static class Assert
{
public static void IsFalse(bool b)
{
if (b)
{
throw new Exception();
}
}

public static void IsTrue(bool b)
{
if (!b)
{
throw new Exception();
}
}
}

public void When_IsTabStop()
{
var customControl = new CustomControl();
Assert.IsTrue(customControl.IsTabStop);

var userControl = new UserControl();
Assert.IsFalse(userControl.IsTabStop);

var customUserControl = new CustomUserControl();
Assert.IsFalse(customUserControl.IsTabStop);

Assert.IsFalse((bool)Control.IsTabStopProperty.GetMetadata(typeof(UIElement)).DefaultValue);
Assert.IsFalse((bool)Control.IsTabStopProperty.GetMetadata(typeof(FrameworkElement)).DefaultValue);
Assert.IsFalse((bool)Control.IsTabStopProperty.GetMetadata(typeof(CustomFE)).DefaultValue);
Assert.IsTrue((bool)Control.IsTabStopProperty.GetMetadata(typeof(Control)).DefaultValue);
Assert.IsFalse((bool)Control.IsTabStopProperty.GetMetadata(typeof(UserControl)).DefaultValue);
Assert.IsFalse((bool)Control.IsTabStopProperty.GetMetadata(typeof(CustomUserControl)).DefaultValue);
Assert.IsTrue((bool)Control.IsTabStopProperty.GetMetadata(typeof(CustomControl)).DefaultValue); // fails
}
}
```

### Expected behavior

The last assert should pass

### Screenshots

_No response_

### NuGet package version

None

### Windows version

_No response_

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start from the DependencyProperty.GetMetadata entry point and reproduce the failure with the CustomControl and CustomUserControl types shown in the issue. Compare the returned default values for Control and CustomControl, then add coverage for the listed assertions so the final CustomControl assertion passes.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.