Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DictionaryKeyPropertyAttribute not working #7562

Open
Kation opened this issue Feb 22, 2023 · 3 comments
Open

DictionaryKeyPropertyAttribute not working #7562

Kation opened this issue Feb 22, 2023 · 3 comments
Labels
Investigate Requires further investigation by the WPF team.
Milestone

Comments

@Kation
Copy link

Kation commented Feb 22, 2023

Description

Class define with DictionaryKeyPropertyAttribute still show All objects added to an IDictionary must have a Key attribute or some other type of key associated with them. error while building project.

Reproduction Steps

Define a class with DictionaryKeyPropertyAttribute.

[DictionaryKeyProperty("TargetType")]
public class MyStyle
{
    public MyStyle()
    {

    }

    public Type TargetType { get; set; }
}

Use it in a xaml file.

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        xmlns:my="clr-namespace:WpfLibrary1;assembly=WpfLibrary1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <ResourceDictionary>
            <my:MyStyle TargetType="{x:Type Butt
[WpfApp1.zip](https://github.com/dotnet/wpf/files/10802917/WpfApp1.zip)
on}"/>
        </ResourceDictionary>
    </Window.Resources>
    <Grid>
    </Grid>
</Window>

Expected behavior

Build successful

Actual behavior

Build failed

Regression?

No response

Known Workarounds

No response

Impact

No response

Configuration

No response

Other information

WpfApp1.zip

@ghost
Copy link

ghost commented Feb 24, 2023

Here's a workaround

<!-- Set x:Key to have the same value as TargetType -->
<my:MyStyle x:Key="{x:Type Button}" TargetType="{x:Type Button}"/>

@Kation
Copy link
Author

Kation commented Feb 24, 2023

@tyl4r I means there have to fix this issure.
It's about xaml intellisense issure.
It can read successful by xaml reader.

@pchaurasia14 pchaurasia14 added the Investigate Requires further investigation by the WPF team. label Feb 27, 2023
@karelz karelz added this to the Future milestone Sep 15, 2023
@Nintynuts
Copy link

Nintynuts commented Nov 19, 2024

I'm also wondering why this isn't working. I've checked the source to work out how it obtains the key from DictionaryKeyPropertyAttribute and can't see why it would work for built-in types but not for custom ones (but it's quite hard to follow, so I might have missed it).

Here seems to be where it gets key from the property named by the attribute, (also here, but doesn't seem to use the value for anything) which throws SR.MissingKey which I can't seem to find, but I can't see anything which restricts the types it works with.

But there are two places where the MC3022 exception SR.ParserNoDictionaryKey is thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Investigate Requires further investigation by the WPF team.
Projects
None yet
Development

No branches or pull requests

4 participants