我有以下资源字典合并到主题/generic.xaml文件中
<DataTemplate DataType="{x:Type model:RequirementResourceRelation}" x:Key="{x:Static local:Resources.RequirementResourceRelationListTemplateKey}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Resource.Name, TargetNullValue=Loading...}" />
<TextBlock Grid.Column="1" Text="-" />
<TextBlock Grid.Column="2" MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Path=RelationType, TargetNullValue=Loading...}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" />
</Grid>
</DataTemplate>
<DataTemplate DataType="{x:Type model:RequirementResourceRelation}" x:Key="{x:Static local:Resources.RequirementResourceRelationListTemplate2Key}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<TextBlock MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Requirement.Name, TargetNullValue=Loading...}" />
<TextBlock Grid.Column="1" Text="-" />
<TextBlock Grid.Column="2" MinWidth="35" HorizontalAlignment="Left" Padding="3,0" Text="{Binding Path=RelationType, TargetNullValue=Loading...}" TextWrapping="NoWrap" TextTrimming="CharacterEllipsis" />
</Grid>
</DataTemplate>
我试图为具有不同组件资源的同一数据类型创建两个不同的数据模板。如您所见,其中一个键附加了一个2。
在我的local:resources类中,我使用了以下组件资源。
public static ComponentResourceKey RequirementResourceRelationListTemplateKey {
get {
return new ComponentResourceKey(typeof(Resources), "RequirementResourceRelationListTemplate");
}
}
public static ComponentResourceKey RequirementResourceRelationListTemplate2Key {
get {
return new ComponentResourceKey(typeof(Resources), "RequirementResourceRelationListTemplate2");
}
}
如果我在其中只有一个数据模板,这是可行的,但是一旦我添加了第二个,就会得到一个异常,它说:
Item has already been added. Key in dictionary: 'DataTemplateKey(HR.TrackingTool.Model.RequirementResourceRelation)' Key being added: 'DataTemplateKey(HR.TrackingTool.Model.RequirementResourceRelation)'
at System.Collections.Hashtable.Insert(Object key, Object nvalue, Boolean add)
at System.Collections.Hashtable.Add(Object key, Object value)
at System.Windows.ResourceDictionary.SetKeys(IList`1 keyCollection, IServiceProvider serviceProvider)
at System.Windows.ResourceDictionary.SetDeferrableContent(DeferrableContent deferrableContent)
at System.Windows.Baml2006.WpfSharedBamlSchemaContext.<Create_BamlProperty_ResourceDictionary_DeferrableContent>b__168(Object target, Object value)
at System.Windows.Baml2006.WpfKnownMemberInvoker.SetValue(Object instance, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(XamlMember member, Object obj, Object value)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
似乎ResourceDictionary在添加数据模板时忽略了键。ResourceDictionary在使用ComponentRelationKey时是否忽略Key属性?
有没有办法解决这个例外?
谢谢,
劳尔