【问题标题】:Is it possible to reference a resource dictionary and define a style in the same resource section?是否可以引用资源字典并在同一资源部分中定义样式?
【发布时间】:2011-07-14 12:39:27
【问题描述】:

像这样:

<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,因为 window 的 Resource 属性已经定义(它不是)。如果我删除这些东西,一切都很好。

有什么想法吗?

【问题讨论】:

    标签: wpf xaml styles resourcedictionary


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-09
      • 1970-01-01
      • 2021-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多