异常:“‘Storyboard’ 上缺少键值对象。”带有样式触发器

发布于 2024-09-14 10:50:28 字数 2484 浏览 6 评论 0原文

我正在重新设计一个按钮,其中包括提供 controlTemplate 并添加一些触发器(用于 mouseenter 和 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>

但是当我运行它时,启动时出现以下错误:

'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}"/>

我想做的就是当鼠标悬停在按钮上时,让按钮的前景淡入不同的颜色。有人知道我哪里出错了吗?我似乎无法理解它。

I'm restyling a button, and that includes providing a controlTemplate and also adding some triggers (for mouseenter and mouseleave).

My XAML is as follows:

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

Which corresponds to this line:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2024-09-21 10:50:28

我看到三个问题。

一种是您将 Storyboard 添加到资源字典中而不指定键。我认为您需要 x:Key 而不是 x:Name

接下来,通过将 Storyboard.Target 设置为与 RelativeSource Self 的绑定,您可以将目标设置为 ColorAnimation 本身。实际上,您可以关闭该属性,默认情况下它将以按钮为目标。

最后,您将 TargetProperty 设置为 Foreground,但您实际上想要为 Foreground 画笔的 Color 属性设置动画,因此您需要使用 Foreground.Color。试试这个:

<Storyboard x:Key="mouseOverText">
    <ColorAnimation
        Storyboard.TargetProperty="Foreground.Color"
        From="{StaticResource ResourceKey=ForeColour}"
        To="{StaticResource ResourceKey=GlowColour}"/>
</Storyboard>

I see three issues.

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.

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.

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文