I have the following ResourceDictionary that gets merged into my Themes/Generic.xaml file
<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>
I'm trying to create two different data templates for the same DataType with different ComponentResourceKey. As you can see one of the keys has a 2 appended to it.
In my local:Resources class I have the following which is the ComponentResourceKey I'm using.
public static ComponentResourceKey RequirementResourceRelationListTemplateKey {
get {
return new ComponentResourceKey(typeof(Resources), "RequirementResourceRelationListTemplate");
}
}
public static ComponentResourceKey RequirementResourceRelationListTemplate2Key {
get {
return new ComponentResourceKey(typeof(Resources), "RequirementResourceRelationListTemplate2");
}
}
This works if I have only one of the DataTemplates in there, but once I add the second one I get an exception that says:
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)
It seems that the ResourceDictionary ignores the key when adding a DataTemplate. Does the ResourceDictionary ignore the key property when it's using a ComponentRelationKey ?
Any way around this exception ?
Thanks, Raul
If you reference your DataTemplate by key, can't you just leave out the DataType-specification? Without DataType="{x:Type model:RequirementResourceRelation}" (which is apparently the key for the added item) your x:Key should be used as the key.