2012-06-07 8 views
6

Mam okno WPF, które ma załadować dwa obrazy wektorowe z plików XAML. (Każdy z nich jest w osobnym pliku dla łatwiejszej zmiany w Expression Design.)Uzyskaj zasób z ResourceDictionary za pomocą klucza

Kiedy zawierać pliki XAML w MergedDictionary, to działa dobrze. Oto kod używam:

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

i

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

Jednak teraz trzeba dodać więcej zasobów okna. Nowy zasób należy do tego okna, więc chcemy, aby znajdował się w tym samym pliku, a nie w dołączonym pliku.

Kiedy dodać następujący kod między <Window.Resources> i <ResourceDictionary>, pojawia się następujący błąd:

Code

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

Warning

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.

Więc zmienić <ResourceDictionary> dodaj do tego:

<ResourceDictionary x:Key="Images"> 

Jednak nie wiem, jak uzyskać dostęp do zasobów w tym słowniku teraz. Jak zdobyć zasoby od wewnątrz o nazwie ResourceDictionary?


EDIT

Nieważne. Ta kompilacja, ale nie działa.

Błąd jest:

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

Chyba będę musiał to zrobić w jakiś inny sposób.

Odpowiedz

8

Według strony MSDN na stronie MergedResourceDictionary's jest prawnie, ale nie jest powszechne definiowanie zasobu w słowniku zasobów określonym w MergedDictionary. Z powyższej strony.

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.

Spróbuj więc sprawdzić, czy działa.

<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

Wygląda na to, że działa, i jest o wiele lepszą odpowiedzią niż moja. –

+0

@MosheKatz Cieszę się, że mogę Ci pomóc –

1

Skończyłem na przeniesieniu pliku <Style> na element nadrzędny elementów, które używają tego stylu.

To nie jest najlepsze rozwiązanie, ale działa.

Powiązane problemy