WPF 中的颜色过渡

问题描述 投票:0回答:5

我想对 WPF 窗口的

Background
颜色进行颜色过渡。

我该怎么做?

例如:

Brush i_color = Brushes.Red; //this is the initial color
Brush f_color = Brushes.Blue; //this is the final color

当我点击

Button
按钮1

private void button1_Click(object sender, RoutedEventArgs e)
{
    this.Background = f_color; //here the transition begins. I don't want to be quick. Maybe an interval of 4 seconds.
}
c# wpf xaml colors transition
5个回答
13
投票

在代码中可以用这个来完成

private void button1_Click(object sender, RoutedEventArgs e)
{
    ColorAnimation ca = new ColorAnimation(Colors.Red, Colors.Blue, new Duration(TimeSpan.FromSeconds(4)));
    Storyboard.SetTarget(ca, this);
    Storyboard.SetTargetProperty(ca, new PropertyPath("Background.Color"));

    Storyboard stb = new Storyboard();
    stb.Children.Add(ca);
    stb.Begin();
}

作为 H.B.指出这也会起作用

private void button1_Click(object sender, RoutedEventArgs e)
{
    ColorAnimation ca = new ColorAnimation(Colors.Blue, new Duration(TimeSpan.FromSeconds(4)));
    this.Background = new SolidColorBrush(Colors.Red);
    this.Background.BeginAnimation(SolidColorBrush.ColorProperty, ca);
}

7
投票

这是一种方法:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid x:Name="BackgroundGrid" Background="Red">

        <Button HorizontalAlignment="Left" VerticalAlignment="Top">
            Transition
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation  Storyboard.TargetName="BackgroundGrid" From="Red" To="Blue" Duration="0:0:4" Storyboard.TargetProperty="Background" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Button.Triggers>
        </Button>
    </Grid>
</Window>

4
投票

您可以使用 动画(阅读本文),特别是

ColorAnimation
(参见示例)或
ColorAnimationUsingKeyframes


2
投票

只是为了完成LPL和H.B.回答..... 就我而言,我需要将控件恢复为动画之前的颜色。

这是我的代码

ColorAnimation animation = new ColorAnimation()
{
    From = Colors.Orange,
    To = ((SolidColorBrush)myControl.Background).Color,//Revert to initial control Color
    Duration = new Duration(TimeSpan.FromSeconds(2))
};

myControl.Background = new SolidColorBrush(Colors.Orange);//Do not use a frozen instance
myControl.Background.BeginAnimation(SolidColorBrush.ColorProperty, animation);

0
投票

您还可以将背景设置为一种颜色,然后将此 xaml 用于窗口以重复更改为指定的其他颜色:

<Window.Triggers>
  <EventTrigger RoutedEvent="Window.Loaded">
    <BeginStoryboard>
        <Storyboard>
            <ColorAnimation To="White" Storyboard.TargetProperty="(Window.Background).(SolidColorBrush.Color)" FillBehavior="Stop" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever"/>
        </Storyboard>
    </BeginStoryboard>
  </EventTrigger>
</Window.Triggers>
© www.soinside.com 2019 - 2024. All rights reserved.