dotnet / dotnet/wpf

Non-string x:Key ignored on DataTemplate, when DataType set.

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

Description

* .NET Core Version: 5.0.100-preview.1.20155.7
* Windows version: Windows 10.0.18363
* Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
* Is this bug related specifically to tooling in Visual Studio? No.

**Problem description:**
According to docs, x:Key can be set to whatever markup extension returns.
This however is not a truth.

**Actual behavior:**

If there is a resource dictionary with the following 2 lines:
```


```
(where `local:ResourceKeys` is an enum.)

These lines will cause an exception: `ArgumentException: Item has already been added. Key in dictionary: 'DataTemplateKey(Framework.ViewModel)' Key being added: 'DataTemplateKey(Framework.ViewModel)'`

Consider also the following lines being read in a resource dictionary:
```



```
If later in the application you'll try to find these resources in code-behind:
```
var theOtherKeyDataTemplateByEnum = Application.Current.TryFindResource(ResourceKeys.TheOtherKey);
var theOtherKeyDataTemplateByString = Application.Current.TryFindResource("TheOtherKey");
var dataTemplateByEnumKeyAndWithDataType = Application.Current.TryFindResource(ResourceKeys.AKey);
```
Then `theOtherKeyDataTemplateByEnum` and `theOtherKeyDataTemplateByString` won't be `null`, while `dataTemplateByEnumKeyAndWithDataType` will.

**Expected behavior:**
I expect that non-string `x:Key` is not ignored when `DataType` is set for a `DataTemplate`.

**Minimal repro:**
[Repro for .NET Core and .NET Framework is here](https://github.com/MaciekSwiszczowski/EnumBugRepro)

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.