dotnet / dotnet/wpf

MarkupExtension in Windows.Resources will fail Control with x:Shared="False"

Open
#8,069 0 comments 0 reactions 1 assignee Claimed by @Kuldeep-MS View on GitHub
Investigate
Dominant language
C#
Stars
7.7k
Forks
1.3k
Avg merge
1d 11h
Merged PRs (30d)
61

Description

### Description

By creating an instance of a subclass of `MarkupExtension` at the beginning of a `ResourceDictionary`, Controls within the same `ResourceDictionary` with `x:Shared="False"` seems not working

### Reproduction Steps

My code is as follows:

```xaml









```

And the class `ClassInheritME` is a plain class inheriting `MarkupExtension`, which looks like:

```C#
using System.Windows.Markup;

namespace WpfApp1
{
[MarkupExtensionReturnType(typeof(ClassInheritME))]
internal class ClassInheritME : MarkupExtension
{
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
}
}
```

### Expected behavior

If everything works fine, there will be three individual `TextBox` in the `StackPanel`.

### Actual behavior

By writing the ClassInheritME at the beginning of `Window.Resources`, the `TextBox` with `x:Shared="False"` seems not working, as there is only be one `TextBox` visible since they are referencing the same one.

However, as long as moving the `ClassInheritME` instance to anywhere but not the beginning of the `Window.Resources` clause, then the problem is solved, and the `x:Shared="False"` works fine by showing all three `TextBox`.

### Regression?

_No response_

### Known Workarounds

_No response_

### Impact

This bug seems to only occur when instantiating a `MarkupExtension` subclass at the very beginning of a `ResourceDictionary`, and then Controls with `x:Shared="False"` fails.

### Configuration

I tried .NET 6.0 and 7.0 on both Windows 10 (22H2) and 11 (22H2)

### Other information

_No response_

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.