Можно ли ссылаться на словарь ресурсов и определять стиль в том же разделе ресурсов? - PullRequest
2 голосов
/ 14 июля 2011

Примерно так:

<Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary x:Key=""whatever" Source="colors.xaml" />
        <Style TargetType="Button">
            <!- button style using colors defined in colors.xaml -->
        </Style>
    </Window.Resources>

    <StackPanel>
        <Button Background="{DynamicResource background1}" Height="50"></Button>
        <Button Background="{DynamicResource background2}" Height="50"></Button>
    </StackPanel>

</Window>

Если я это сделаю, я получу предупреждения о том, что background1 и background2 не разрешены, и XamlParseException, потому что свойство Resource окна уже определено (это не так).Все хорошо, если я уберу материал.

Есть идеи?

1 Ответ

3 голосов
/ 14 июля 2011

Это легко с MergedDictionaries

<Window.Resources>
   <ResourceDictionary>
     <ResourceDictionary.MergedDictionaries>
       <ResourceDictionary x:Key=""whatever" Source="colors.xaml" /> 
    </ResourceDictionary.MergedDictionaries>
    <Style TargetType="Button">
         <!- button style using colors defined in colors.xaml -->
    </Style>
  </ResourceDictionary>
</Window.Resources>
...