XAML WPF中的颜色过渡

ee7vknir  于 12个月前  发布在  其他
关注(0)|答案(5)|浏览(169)

我想做一个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.
}

wooyq4lh

wooyq4lh1#

在代码中,它可以这样做:

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);
}

eeq64g8w

eeq64g8w2#

这里有一个方法:

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

字符串

wwtsj6pe

wwtsj6pe3#

您可以使用animation(阅读本文),特别是ColorAnimation(参见示例)或ColorAnimationUsingKeyframes

hsgswve4

hsgswve44#

只是为了完成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);

字符串

2g32fytz

2g32fytz5#

你也可以将背景设置为一种颜色,然后将这个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>

字符串

相关问题