0

This question has been asked before, but the answers aren't working. Based on what I read here, this should work.

<ResourceDictionary>
      <LinearGradientBrush x:Key="emerald" EndPoint="1,0.5" StartPoint="0,0.5">
        <GradientStop Color="#FF00FD8A" Offset="0"/>
        <GradientStop Color="#FF4DBD65" Offset="1"/>
      </LinearGradientBrush>
      <StaticResourceExtension x:Key="MainPanelBackground" ResourceKey="emerald"/>

    </ResourceDictionary>

followed by

 <Frame x:Name="_mainFrame" Background="{StaticResource MainPanelBackground}" />

However, what I get is the XAML previewer complaining

  An object of the type "System.Windows.StaticResourceExtension" cannot be 
  applied to a property that expects the type "System.Windows.Media.Brush". 

Running the program just gives a XAML parse error.

As reference, here is another question that purports to have the answer, but this doesn't work for me, and I can't figure out why: Redefine/alias a resource in WPF?

Community
  • 1
  • 1
Alan Baljeu
  • 2,383
  • 4
  • 25
  • 40
  • I am getting the same error as you when compiling but the project actually builds successfully and when I run the program it works as expected. Did you check to see if your build is successful? – Jakob Christensen Feb 02 '14 at 18:16
  • I build and I get a XAML parse error at runtime. The problem goes away if I get rid of {StaticResource MainPanelBackground} – Alan Baljeu Feb 02 '14 at 18:20
  • If you read the very next answer to the question you linked, you'll see that they describe the same thing you do. The difference between what you are doing and what is shown in the answer you linked is that the alias is defined outside the resource dictionary. – Lee O. Feb 02 '14 at 18:25
  • Try moving your StaticResourceExtension element to a different resourcedictionary. – Jakob Christensen Feb 02 '14 at 18:29
  • Jakob, I had tried that with no effect. Lee O: You mean in a different ResourceDictionary file? Didn't work. – Alan Baljeu Feb 02 '14 at 18:33
  • What are you trying to accomplish? You want to make it so if you change the color that you want displayed for mainpanelbackground you could just change the resourcekey of the alias and not have to change all the places you are using it? – Lee O. Feb 02 '14 at 18:42
  • I'll be interested to see if someone gives a solution for this. The way I'd do it is to use a style but that would require a few more spots to make changes than you are aiming for but still less than using the brush directly. – Lee O. Feb 02 '14 at 19:12
  • What version of .net (therefore xaml) are you using? – Gusdor Feb 03 '14 at 09:23
  • Project settings indicate I'm using WPF 4.0. Given that, are these imports correct?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" – Alan Baljeu Feb 03 '14 at 15:03

1 Answers1

4

Use <DynamicResourceExtension ... instead of <StaticResourceExtension ...? There seems to be problems creating aliases using the StaticResourceExtension approach in global resource dictionaries although I cannot explain why.

iobus
  • 41
  • 5