2016-07-27 212 views
1

我需要有一個單一的文件資源與各種資源字典與關鍵獲得來自各種資源字典資源字典資源一鍵

下一個代碼資源字典元素就是這樣一個樣本:

<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

我如何通過「Configuration」鍵訪問ResourceDictionary中包含的資源_Conf_Delete_instr?用轉換器?

謝謝。

+1

嘗試拼圖您的其他ResourceDictionarys在'MergedDictionaries'裏 – lokusking

+0

嗨。謝謝你的回覆。我需要一個資源文件。如果我在mergeddictionaris中嵌套我的其他resourceDictionarys元素(帶鍵),我有這個錯誤:「該鍵屬性只能用於嵌入在類型Idictionary屬性中的標籤」。謝謝。 – Javier

回答

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

+0

謝謝lokusking。完善!。謝謝回覆 – Javier