【问题标题】:Get a Resource from a ResourceDictionary with various Resource Dictionary with a key从具有各种资源字典的 ResourceDictionary 中获取资源
【发布时间】:2016-07-27 17:41:02
【问题描述】:

我需要将单个文件中的资源放入一个 ResourceDictionary 元素中,其中包含各种带有 key 的 resourceDictionary

下一个代码是这样的示例:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/ presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:sys="clr-namespace:System;assembly=mscorlib">

  <ResourceDictionary x:Key="Configuration">
        <sys:String x:Key="_Conf_Delete_instr">Delete instrument</sys:String>
  </ResourceDictionary>
<ResourceDictionary x:Key="Report">
        <sys:String x:Key="mykey2">myvalue2</sys:String>
  </ResourceDictionary>
</ResourceDictionary>


I wonder if when defining the content of an element can access the ResourceDictionary indicating the key:

   <Button x:Name="btnDeleteInst" Content="{DynamicResource _Conf_Delete_instr}" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="0,23,245,0" HorizontalAlignment="Right" Height="50" MinWidth="100" VerticalContentAlignment="Center" Click="btnDeleteInstr_Click"/> 

前面的代码在 _Conf_Delete_instr 上抛出错误,因为找不到 Key

如何使用“配置”键访问 ResourceDictionary 中包含的资源 _Conf_Delete_instr?有转换器吗?

谢谢。

【问题讨论】:

  • 尝试将您的其他 ResourceDictionarys 嵌套在 MergedDictionaries
  • 嗨。谢谢你的回复。我需要一个资源文件。如果我在mergeddictionaris 中嵌套我的其他resourceDictionarys 元素(带键),则会出现此错误:“键属性只能用于嵌入在Idictionary 类型的属性中的标记中”。谢谢。

标签: c# .net wpf xaml resourcedictionary


【解决方案1】:

举个例子:

  <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary x:Name="Configuration">
                    <sys:String x:Key="_Conf_Delete_instr">Delete instrument</sys:String>
                </ResourceDictionary>
                <ResourceDictionary x:Name="Report">
                    <sys:String x:Key="mykey2">myvalue2</sys:String>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>

测试

<Button x:Name="btnDeleteInst" Content="{DynamicResource mykey2}" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="0,23,245,0" HorizontalAlignment="Right" Height="50" MinWidth="100" VerticalContentAlignment="Center" />

<Button x:Name="btnDeleteInst1" Content="{DynamicResource _Conf_Delete_instr}" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="0,23,245,0" HorizontalAlignment="Right" Height="50" MinWidth="100" VerticalContentAlignment="Center" />

结果

【讨论】:

    最近更新 更多