ColorAnimation不会触发

时间:2016-04-28 04:06:59

标签: c# xaml silverlight

我尝试为我的模板的LinearGradientBrush设置动画。如果执行了单击,我想更改画布背景的颜色。 "按下"事件被解雇了,但没有任何反应。

        <ControlTemplate x:Key="ButtonTemplate" TargetType="Button">
        <Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                    <VisualState x:Name="Normal"/>
                    <VisualState x:Name="MouseOver" />
                    <VisualState x:Name="Pressed">
                        <Storyboard AutoReverse="True" Duration="0:0:0.5">
                            <ColorAnimation Storyboard.TargetName="Form" Storyboard.TargetProperty="(Canvas.Background).(LinearGradientBrush.GradientStops)[0].(GradientStop.Color)" To="Red" Duration="0:0:0.5" />
                            <ColorAnimation Storyboard.TargetName="Form" Storyboard.TargetProperty="(Canvas.Background).(LinearGradientBrush.GradientStops)[1].(GradientStop.Color)" From="Black" To="Red" Duration="0:0:0.5" />
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Disabled" />
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>

            <Canvas Name="Form" Height="35">
                <Canvas.Background>
                    <LinearGradientBrush EndPoint="0.5,0.5" StartPoint="0.5,0">
                        <GradientStop Color="#FF007FEA" Offset="0"/>
                        <GradientStop Color="#FF004580" Offset="1"/>
                    </LinearGradientBrush>
                </Canvas.Background>
            </Canvas>

            <ContentPresenter Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" />
        </Grid>
    </ControlTemplate>

1 个答案:

答案 0 :(得分:1)

我在那里看到x:Key。如果这只是您的Button样式模板中的剪辑,那么只需删除该x:Key,但是如果将此ControlTemplate用作资源,那么您只需点击错误的TargetProperty我非常确定Canvas是Panel.,您只需替换Storyboard.TargetProperty并丢失动画中的From attrib,如工作ColorAnimation&#39;在下面。

<ColorAnimation Duration="0" To="Red" 
                Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[0].(GradientStop.Color)" 
                Storyboard.TargetName="Form"/>
<ColorAnimation Duration="0" To="Black" 
                Storyboard.TargetProperty="(Panel.Background).(GradientBrush.GradientStops)[1].(GradientStop.Color)" 
                Storyboard.TargetName="Form"/>