已使用 ComponentResourceKey 添加 WPF ResourceDictionary 项
我有以下 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");
}
}
如果我只有其中一个 DataTemplate,则此方法有效,但是一旦添加第二个 DataTemplate,我就会收到一个异常:
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 属性?
有办法解决这个异常吗?
谢谢, 劳尔
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
显然这个问题就是如上所述的。样式声明的顺序很重要。
当同一 TargetType 的两个样式的第一个属性是 TargetType 时
,您会收到错误消息。它似乎忽略了 Key: 属性,并且如上所述使用 TargetType 值作为字典键,例如“{x:Type TextBlock}”
当相同 TargetType 的两个样式的第一个元素是 x:Key,那么你就不用了,如下图。
我想,如果你把垃圾搬来搬去也没关系。明智的做法是,始终以 x:Key 开头,但这是一个非常愚蠢的错误。
Apparently the issue is as stated, sort of. The order of the declaration of the style matters.
When the first attribute for two styles for the same TargetType is the TargetType e.g.
then you get the error. It seems to ignore the Key: attribute and as stated uses the TargetType value as the dictionary key e.g. "{x:Type TextBlock}"
When the first element of two styles for the same TargetType is the x:Key, then you do not, as shown below.
Doesn't matter if you move the crap around I guess. WOrd to the wise, always start with the x:Key, but this is a really stupid error.
如果您通过键引用 DataTemplate,难道不能省略 DataType 规范吗?
没有
DataType =“{x:类型模型:RequirementResourceRelation}”
(这显然是添加项目的密钥)您的 x:Key 应该用作密钥。
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.
将
DataTemplate
移动到另一个控件的
元素内。在 Silverlight 中,这工作得很好:
但在 WPF 中,我必须在控件内移动 3 个模板:
现在它可以在 WPF 和 Silverlight 中工作,不会出现错误。
Move the
DataTemplate
(s) inside the<Resources>
element of another control.In Silverlight this worked just fine:
BUT in WPF, I had to move the 3 templates inside the control:
Now it works in both WPF and Silverlight without the error.