找不到资源到ResourceDictionary

问题描述

我有一个常见的ResourceDictionary,它使用Microsoft示例来使用黑色的ComboBox Microsoft Exemple

在执行过程中引发了一个异常: 例外:找不到名为“ normalBorderBrush”的资源。资源名称区分大小写。

我只想在我的公共ResourceDictionary xaml文件中声明此组合框

<!-- Combo Box-->
<ControlTemplate x:Key="ComboBoxToggleButton" targettype="ToggleButton">

<Border
      x:Name="Border" 
      Grid.ColumnSpan="2"
      CornerRadius="2"
      ...
      BorderBrush="{StaticResource normalBorderBrush}"
      BorderThickness="1" />
...
<!-- Border Brushes -->
    <LinearGradientBrush x:Key="normalBorderBrush" StartPoint="0,0" EndPoint="0,1">
        <GradientBrush.GradientStops>
            <GradientStopCollection>
                <GradientStop Color="#CCC" Offset="0.0"/>
                <GradientStop Color="#444" Offset="1.0"/>
            </GradientStopCollection>
        </GradientBrush.GradientStops>
    </LinearGradientBrush>

normalBorderBrush在同一文件中声明! 我在做什么错了?

先谢谢了。 埃里克

解决方法

定义资源的顺序很重要。

XAML编译器从上到下处理文件,以便能够在NormalBorderBrush中引用ControlTemplate,您需要在定义模板之前先定义画笔

 <LinearGradientBrush x:Key="NormalBorderBrush" ... />

 <ControlTemplate x:Key="ComboBoxToggleButton" ... />