如何在ResourceDictionary中显式声明<staticresource>?

时间:2016-12-09 16:39:25

标签: wpf xaml resourcedictionary staticresource

我试图在<Style.Resources>元素中显式声明一个StaticResource,但是当我尝试运行该应用程序时出现此错误:

  

无法转换类型为#System; Windows.Media.Effects.DropShadowEffect&#39;的对象输入&#39; System.Windows.ResourceDictionary&#39;。

以下是相关代码:

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

如果在错误消息提示之后,我将<StaticResource/>包裹在ResourceDictionary中,我会得到一个&#34;缺少密钥&#34;错误:

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

在&#39; StaticResourceHolder&#39;上缺少键值对象

1 个答案:

答案 0 :(得分:1)

您已经关闭,但您在示例中尝试执行的操作是为Effect的属性提供资源,其中DropShadowEffect {{1}已经。

你真正想做的是这个;

StaticResource

或者,如果你真的希望它嵌入在实例中,那么;

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

希望这会有所帮助,欢呼。

相关问题