代码之家  ›  专栏  ›  技术社区  ›  Nick Udell

异常:序列图像板对象缺少键值。使用样式化触发器

  •  0
  • Nick Udell  · 技术社区  · 16 年前

    我正在重新设置一个按钮的样式,这包括提供一个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}"/>
    

    1 回复  |  直到 15 年前
        1
  •  3
  •   Quartermeister    16 年前

    我看到三个问题。

    一种是在不指定键的情况下将情节提要添加到资源字典中。我想你想要 x:Key x:Name .

    Storyboard.Target 与…结合 RelativeSource Self ,则将目标设置为ColorAnimation本身。实际上,您可以关闭该属性,默认情况下它将以按钮为目标。

    最后,将TargetProperty设置为 Foreground ,但实际上您希望为前景笔刷的“颜色”属性设置动画,因此需要使用 Foreground.Color . 试试这个:

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