【问题标题】:ColorAnimation does not triggerColorAnimation 不触发
【发布时间】:2016-08-22 15:36:23
【问题描述】:

我尝试为我的模板的 LinearGradientBrush 设置动画。如果执行了单击,我想更改画布背景的颜色。 “Pressed”事件被触发,但没有任何反应。

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

【问题讨论】:

    标签: c# xaml silverlight


    【解决方案1】:

    我在那里看到一个x:Key。如果这只是您的 Button 样式模板的一个片段,那么只需删除该 x:Key,但是如果此 ControlTemplate 被用作资源,那么您只是打错了 TargetProperty,因为我很确定 Canvas是Panel.,您可以替换Storyboard.TargetProperty 并丢失动画中的From 属性,如下面的ColorAnimation 所示。

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

    【讨论】:

    • 是的,它被用作静态资源。和 Panel.Background 作品。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 2021-11-14
    • 2012-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多