1

I am trying to declare a StaticResource explicitly inside a <Style.Resources> element, but am getting this error when I try to run the application:

Unable to cast object of type 'System.Windows.Media.Effects.DropShadowEffect' to type 'System.Windows.ResourceDictionary'.

Here is the relevant code:

<DropShadowEffect x:Key="Sombra" Opacity="0.5" ShadowDepth="3" BlurRadius="5"/>

<Style x:Key="BotãoNavegaçãoBase" TargetType="{x:Type ButtonBase}" BasedOn="{StaticResource BotãoGeometria}">
    <Style.Resources>
        <StaticResource x:Key="PathShadow" ResourceKey="Sombra"/>
    </Style.Resources>      
</Style>

And if, following the error message hint, I wrap the <StaticResource/> in a ResourceDictionary, I get a "missing key" error:

    <Style.Resources>
        <ResourceDictionary>
            <StaticResource x:Key="PathShadow" ResourceKey="Sombra"/>
        </ResourceDictionary>
    </Style.Resources>

Missing key value on 'StaticResourceHolder' object.

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
  • That's not what `StaticResource` is for. @ChrisW. posted his answer while I was still testing the same exact answer for multiple uses of the resource. Works fine. If you needed to create multiple instances of the same resource, `3 – 15ee8f99-57ff-4f92-890c-b56153 Dec 09 '16 at 16:50
  • @EdPlunkett I saw a lot of your answers around this issue in SO, and I was pretty confident to have understood it, and that my code above should run fine. I forward you the same question I still have: Why am I getting the exception at runtime, while most folks complain it's mostly a design-time issue? Any thoughts? – heltonbiker Dec 09 '16 at 16:56
  • 1
    @EdPlunkett also, you mention "that's not what StaticResource is for". What exactely do you mean by it? That is, what part of my example is _not_ what StaticResource is for? You can have a broader idea of my intentions in [my former question](http://stackoverflow.com/questions/41062079/error-when-using-staticresourceextension-inside-style-resources) – heltonbiker Dec 09 '16 at 16:58
  • I was referring to using `StaticResource` to insert an existing resource into another `ResourceDictionary`. I've never done that. – 15ee8f99-57ff-4f92-890c-b56153 Dec 09 '16 at 17:01
  • Can you point me to examples of other people doing the same thing with ` – 15ee8f99-57ff-4f92-890c-b56153 Dec 09 '16 at 17:03
  • @EdPlunkett my original motivation came from this answer (and to be honest I think I confused your username with the one from the linked post, sorry for that): http://stackoverflow.com/a/11821239/401828 – heltonbiker Dec 09 '16 at 17:26
  • H.B. said on that one he hadn't checked if it worked. I'd have said it was worth trying, but now we've tried it and it doesn't work. There are other ways to put the same drop shadow on two different controls. I don't (clearly) have a good mental model of why it's not working though. – 15ee8f99-57ff-4f92-890c-b56153 Dec 09 '16 at 18:28
  • @EdPlunkett: I did not check the first example, i did check the second one though. The problem is that i presumably checked it via Kaxaml, which compiles the XAML at run time which means it is not converted to an intermediate format. I just tried it in a compiled project: It fails if compiled statically but works if compiled dynamically. – H.B. Dec 09 '16 at 20:37
  • Closed because you should have just edited the original question, this is not significantly different. – H.B. Dec 09 '16 at 20:53

1 Answers1

1

You're close but what you're trying to do in your example is provide a resource to the property of Effect wherein your DropShadowEffect is the StaticResource already.

What you actually want to do is this;

<DropShadowEffect x:Key="Sombra" Opacity="0.5" ShadowDepth="3" BlurRadius="5"/>

<Style x:Key="BotãoNavegaçãoBase" TargetType="{x:Type ButtonBase}" 
       BasedOn="{StaticResource BotãoGeometria}">
    <Setter Property="Effect" Value="{StaticResource Sombra}"/>     
</Style>

Or if you genuinely want it embedded in the instance, this;

<Style x:Key="BotãoNavegaçãoBase" TargetType="{x:Type ButtonBase}" 
       BasedOn="{StaticResource BotãoGeometria}">
    <Style.Resources>
       <DropShadowEffect x:Key="Sombra" Opacity="0.5" ShadowDepth="3" BlurRadius="5"/>
    </Style.Resources>
    <Setter Property="Effect" Value="{StaticResource Sombra}"/>     
</Style>

Hope this helps, cheers.

Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • Well, not quite. I am trying to declare a StaticResource inside a Style that uses a Template. That template has a DynamicResource. I want _each_ style using it to have the DynamicResourceKey evaluate to a different StaticResource. It can be fully seen in my [previous question](http://stackoverflow.com/questions/41062079/error-when-using-staticresourceextension-inside-style-resources) – heltonbiker Dec 09 '16 at 16:48
  • I tried your second approach and it worked! Still, two issues remain: 1) I have seen the syntax I used initially in other SO posts where they are expected to work; 2) If I am to use the same shadow effect in more than one style, I still would want to have a staticresource that just "shunts" to another defined above, like [this](http://stackoverflow.com/a/5797493/401828). My doubt is: why my former syntax didn't work? Why did I get the Exception? – heltonbiker Dec 09 '16 at 16:52
  • Really? I wouldn't expect that to work at all? I mean you're essentially trying to use something whose whole purpose is to give a value of a property and apparently expecting it to magically inherit to a dependency property that isn't even specified to be begin with? Or from the docs... `Provides a value for any XAML property attribute by looking up a reference to an already defined resource.` However, if I'm reading your example in the comment right, by "shunt" do you mean you want to do some resource chaining like they did with the color? – Chris W. Dec 09 '16 at 17:08
  • Yeah, I think "chaining" is a better word. I had the impression that they just wanted to create aliases in that way. By doing so, I can have something expected a DynamicResource with, say `MagicKey` as ResourceKey, and then "inject" locally any resource already with its own semantic key, but "stamping" it with the desired alias key (`MagicKey`) in this case) instead of redeclaring the same resource over and over everywhere I want its value to be looked up by `MagicKey` dynamic resource key. (phew, that was a mouthful...) – heltonbiker Dec 09 '16 at 17:31