例外:“'Storyboard'对象上的键值缺失。”带样式的触发器 [英] Exception: “Missing key value on ‘Storyboard’ object.” with styled triggers
问题描述
我正在重新设置按钮的样式,其中包括提供controlTemplate并还添加了一些触发器(用于mouseenter和mouseleave)。
I'm restyling a button, and that includes providing a controlTemplate and also adding some triggers (for mouseenter and mouseleave).
我的XAML如下:
<Style TargetType="Button">
<Style.Resources>
<Color x:Key="ForeColour" R="128" G="128" B="128" A="255"/>
<Color x:Key="BackColour" R="211" G="211" B="211" A="255"/>
<Color x:Key="GlowColour" R="30" G="144" B="255" A="255"/>
<SolidColorBrush Color="{StaticResource ResourceKey=ForeColour}" x:Key="ForeBrush"/>
<LinearGradientBrush x:Key="BackBrush" StartPoint="0,1" EndPoint="0,0">
<GradientStop Color="White" Offset="0.5"/>
<GradientStop Color="{StaticResource ResourceKey=BackColour}" Offset="1"/>
</LinearGradientBrush>
<Storyboard x:Name="mouseOverText">
<ColorAnimation Storyboard.Target="{Binding RelativeSource= {RelativeSource Self}" Storyboard.TargetProperty="Foreground" From="{StaticResource ResourceKey=ForeColour}" To="{StaticResource ResourceKey=GlowColour}"/>
</Storyboard>
</Style.Resources>
<Setter Property="Foreground" Value="{StaticResource ResourceKey=ForeBrush}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border BorderBrush="LightGray" BorderThickness="2" CornerRadius="8" Background="{StaticResource ResourceKey=BackBrush}">
<ContentPresenter x:Name="cnpContent" Opacity="1" VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard Storyboard="{StaticResource mouseOverText}"/>
</EventTrigger>
</Style.Triggers>
</Style>
但是当我运行它时,在启动时出现以下错误:
but when I run it I get the following error on startup:
'Missing key value on 'Storyboard' object.' Line number '88' and line position '31'.
与以下行相对应:
<ColorAnimation Storyboard.Target="{Binding RelativeSource= {RelativeSource Self}" Storyboard.TargetProperty="Foreground" From="{StaticResource ResourceKey=ForeColour}" To="{StaticResource ResourceKey=GlowColour}"/>
我要做的就是当鼠标移到按钮的前景色时淡入另一种颜色悬停在它上面。有人知道我要去哪里了吗?我似乎无法理解。
All I'm trying to do is get the button's foreground to fade to a different colour when the mouse hovers over it. Anybody have any idea where I'm going wrong? I can't seem to fathom it.
推荐答案
我看到了三个问题。
一个是您要在没有指定键的情况下将情节提要添加到资源字典中。我想您想要的是 x:Key
而不是 x:Name
。
One is that you are adding the Storyboard to a resource dictionary without specifying a key. I think you want x:Key
instead of x:Name
.
接下来是通过将 Storyboard.Target
设置为与 RelativeSource Self
,您将目标设置为ColorAnimation本身。实际上,您可以只关闭该属性,并且默认情况下它将定位到Button。
The next is that by setting Storyboard.Target
to a binding with RelativeSource Self
, you're setting the target to the ColorAnimation itself. You can actually just leave that attribute off and it will target the Button by default.
最后,将TargetProperty设置为 Foreground
,但是您实际上想为前景的Color属性设置动画画笔,因此您需要使用 Foreground.Color
。尝试以下操作:
Finally, you have the TargetProperty set to Foreground
, but you actually want to animate the Color property of the Foreground brush, so you need to use Foreground.Color
. Try this:
<Storyboard x:Key="mouseOverText">
<ColorAnimation
Storyboard.TargetProperty="Foreground.Color"
From="{StaticResource ResourceKey=ForeColour}"
To="{StaticResource ResourceKey=GlowColour}"/>
</Storyboard>
这篇关于例外:“'Storyboard'对象上的键值缺失。”带样式的触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!