Eric
Eric

Reputation: 39

Cannot find resource into ResourceDictionary

I've got a common ResourceDictionary that use an Microsoft example for use a black ComboBox Microsoft Exemple

In execution a exception was thrown : Exception: Cannot find resource named 'NormalBorderBrush'. Resource names are case sensitive.

I just want to declare this combobox in my common ResourceDictionary xaml file

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

The NormalBorderBrush is declared in this same file ! What Am I doing wrong ?

Thanks in advance. Eric

Upvotes: 0

Views: 126

Answers (1)

mm8
mm8

Reputation: 169420

The order in which you define the resources matters.

The XAML compiler processes the file from top to bottom so to be able to reference NormalBorderBrush in the ControlTemplate, you need to define the brush before you define the template:

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

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

Upvotes: 2

Related Questions