2012-06-07 4 views
6

Ho una finestra WPF che dovrebbe caricare due immagini vettoriali da file XAML. (Ciascuno è in un file separato per semplificare la modifica in Expression Design.)Ottenere una risorsa da un ResourceDictionary con una chiave

Quando includo i file XAML in un MergedDictionary, funziona correttamente. ecco il codice che uso:

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Images/LCCD logo.xaml" /> 
      <ResourceDictionary Source="Images/LCCD bottom image.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

e

<Image Source="{Binding Source={StaticResource LCCDlogo}}" /> <!-- Simplified --> 
<Image Source="{Binding Source={StaticResource LCCDbar}}" /> <!-- Simplified --> 

Tuttavia, ora bisogno di aggiungere di più per le risorse della finestra. La nuova risorsa appartiene a questa finestra, quindi vogliamo che sia nello stesso file anziché in un file incluso.

Quando aggiungo il seguente codice tra <Window.Resources> e <ResourceDictionary>, ottengo il seguente errore:

Codice

<Style TargetType="{x:Type tab:FabTabItem}"> 
     <Setter Property="Header" Value="{Binding Path=LabelText}"/> 
     <Setter Property="HeaderTemplate"> 
      <Setter.Value> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="0,0,4,0"> 
         <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/> 
        </StackPanel> 
       </DataTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

Attenzione

The designer does not support loading dictionaries that mix 'ResourceDictionary' items without a key and other items in the same collection. Please ensure that the 'Resources' property does not contain 'ResourceDictionary' items without a key, or that the 'ResourceDictionary' item is the only element in the collection.

Così ho cambiare la <ResourceDictionary> tag a questo:

<ResourceDictionary x:Key="Images"> 

Tuttavia, non so come accedere alle risorse all'interno di questo dizionario ora. Come si ottengono le risorse dall'interno di un numero ResourceDictionary?


EDIT

Non importa. Questo compila ma non funziona.

L'errore è:

''Resources' property has already been set on 'MainWindow'.

mi sa che dovrò farlo in qualche altro modo.

risposta

8

In base alla pagina MSDN su MergedResourceDictionary's è legale ma non comune definire una risorsa in un dizionario risorse specificato in MergedDictionary. Dalla pagina precedente.

It is legal to define resources within a ResourceDictionary that is specified as a merged dictionary, either as an alternative to specifying Source, or in addition to whatever resources are included from the specified source. However, this is not a common scenario; the main scenario for merged dictionaries is to merge resources from external file locations. If you want to specify resources within the markup for a page, you should typically define these in the main ResourceDictionary and not in the merged dictionaries.

Quindi provate a vedere se funziona.

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Images/LCCD logo.xaml" /> 
      <ResourceDictionary Source="Images/LCCD bottom image.xaml" /> 
      <ResourceDictionary> 
       <Style TargetType="{x:Type tab:FabTabItem}"> 
        <Setter Property="Header" Value="{Binding Path=LabelText}"/> 
        <Setter Property="HeaderTemplate"> 
         <Setter.Value> 
          <DataTemplate> 
           <StackPanel Orientation="Horizontal" Margin="0,0,4,0"> 
            <TextBlock Text="{Binding}" TextTrimming="CharacterEllipsis"/> 
           </StackPanel> 
          </DataTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 
+0

Sembra che funzioni, ed è una risposta molto migliore che mia. –

+0

@MosheKatz Felice di aiutare –

1

Ho finito per spostare il <Style> più in basso nel file, sull'elemento padre degli elementi che usano quello stile.

Non è la soluzione migliore, ma funziona.