wpf WPF项目中每个字典条目必须有一个关联的键错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26872929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Each dictionary entry must have an associated key error in WPF project
提问by MikaelKP
I get the following error in my App.xaml file: "Each dictionary entry must have a key"
我在 App.xaml 文件中收到以下错误:“每个字典条目必须有一个键”
My App.xaml looks like this:
我的 App.xaml 看起来像这样:
<Application.Resources>
<DataTemplate DataType="{x:Type Model:ClassData}">
<Canvas>
<View:ClassDataUserControl/>
</Canvas>
</DataTemplate>
<ResourceDictionary> <----- ERROR HERE
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/UMLDiagram.Windows.Theme;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
<DataTemplate DataType="{x:Type Model:Connector}">
<Canvas>
<View:ConnectorUserControl/>
</Canvas>
</DataTemplate>
</Application.Resources>
My Resource Dictionary (ButtonStyle.xaml) is placed in another project called UMLDiagram.Windows.Theme and the code looks like this:
我的资源字典 (ButtonStyle.xaml) 放在另一个名为 UMLDiagram.Windows.Theme 的项目中,代码如下所示:
<Style TargetType="Button">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FF456DB4" Offset="1"/>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Style>
To make the software scaleable the ButtonStyle.xaml is called via Theme.xaml (also placed in UMLDiagram.Windows.Theme project)
为了使软件可缩放,ButtonStyle.xaml 通过 Theme.xaml 调用(也放置在 UMLDiagram.Windows.Theme 项目中)
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ButtonStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
I have tried everything and I cannot see why I see the error!
我已经尝试了一切,但我不明白为什么我会看到错误!
Can anybody please help me with this problem?
有人可以帮我解决这个问题吗?
Best regards, Mikael
最好的问候,迈克尔
回答by Michael G
You need to put your DataTemplateinside of the ResourceDictionary.
你需要把你的DataTemplate里面ResourceDictionary。
When you create a new resource dictionary reference as a resource, all resources must be contained in that resource dictionary.
当您创建新的资源字典引用作为资源时,所有资源都必须包含在该资源字典中。
You can read more about using the merged resource dictionary here.
您可以在此处阅读有关使用合并资源字典的更多信息。
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/UMLDiagram.Windows.Theme;component/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
<DataTemplate DataType="{x:Type Model:ClassData}">
<Canvas>
<View:ClassDataUserControl/>
</Canvas>
</DataTemplate>
<DataTemplate DataType="{x:Type Model:Connector}">
<Canvas>
<View:ConnectorUserControl/>
</Canvas>
</DataTemplate>
</ResourceDictionary>
</Application.Resources>

