使用带有密钥的各种资源字典从ResourceDictionary获取资源

时间:2016-07-27 17:41:03

标签: c# .net wpf xaml resourcedictionary

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

的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

如何使用键&#34;配置&#34;来访问ResourceDictionary中包含的资源_Conf_Delete_instr?有转换器?

感谢。

1 个答案:

答案 0 :(得分:0)

举个例子:

  <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" />

<强>结果

Result