XAML 我需要在WPF中动画颜色变化

e4eetjau  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(93)

我需要颜色按顺序变化。让我们说从灰色到红色,然后从红色到绿色......等等。
到目前为止,我得到了这个:

<UserControl.Resources>
    <Style x:Key="BlinkingStyle" TargetType="Button">
        <Style.Triggers>
            <DataTrigger Binding="{Binding AnimationMode}" Value="BlinkingOn">
                <DataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Lime" AutoReverse="True" RepeatBehavior="Forever" Duration="0:0:5" />
                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" From="Lime" To="Red" AutoReverse="True" RepeatBehavior="Forever" Duration="0:0:5" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.EnterActions>
                <DataTrigger.ExitActions>
                    <BeginStoryboard Name="StopFlashingStoryboard">
                        <Storyboard>
                            <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="LightGray" Duration="0:0:2" />
                        </Storyboard>
                    </BeginStoryboard>
                </DataTrigger.ExitActions>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>

<Grid>
    <Border BorderBrush="Black" BorderThickness="1">
        <Button Command="{Binding ItemSelectedCommand}" CommandParameter="{Binding}" Style="{StaticResource BlinkingStyle}">
        </Button>
    </Border>
</Grid>

颜色行为似乎是并行的。颜色在运行时混合在一起。我需要的颜色改变到Lime超过5秒,然后从LimeRed超过未来5秒,然后到LightGray为2秒。

jexiocij

jexiocij1#

我需要的颜色改变为石灰超过5秒,然后从石灰红色超过未来5秒,然后到浅灰色2秒。
你可以像这样使用ColorAnimationUsingKeyFrames

<Style x:Key="BlinkingStyle" TargetType="Button">
    <Style.Triggers>
        <DataTrigger Binding="{Binding AnimationMode}" Value="BlinkingOn">
            <DataTrigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)">
                            <LinearColorKeyFrame Value="Lime" KeyTime="0:0:5"/>
                            <LinearColorKeyFrame Value="Red" KeyTime="0:0:10"/>
                            <LinearColorKeyFrame Value="LightGray" KeyTime="0:0:12"/>
                        </ColorAnimationUsingKeyFrames>
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

相关问题