我有以下 ResourceDictionary 已合并到我的 Themes/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>
我正在尝试为具有不同 ComponentResourceKey 的相同 DataType 创建两个不同的数据模板。如您所见,其中一个键附加了一个 2。
在我的 local:Resources 类中,我有以下内容,这是我正在使用的 ComponentResourceKey。
public static ComponentResourceKey RequirementResourceRelationListTemplateKey {
get {
return new ComponentResourceKey(typeof(Resources), "RequirementResourceRelationListTemplate");
}
}
public static ComponentResourceKey RequirementResourceRelationListTemplate2Key {
get {
return new ComponentResourceKey(typeof(Resources), "RequirementResourceRelationListTemplate2");
}
}
如果我在那里只有一个 DataTemplates,这很有效,但是一旦我添加了第二个,我就会得到一个异常,上面写着:
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)
添加 DataTemplate 时,ResourceDictionary 似乎忽略了键。ResourceDictionary 在使用 ComponentRelationKey 时是否忽略 key 属性?
有什么办法可以解决这个异常吗?
谢谢,劳尔