在WPF的色彩过渡 [英] Color transition in WPF

查看:167
本文介绍了在WPF的色彩过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个WPF窗口的背景颜色的颜色过渡。



如何我能做到这一点



例如:

 刷i_color = Brushes.Red; //这是初始颜色
刷f_color = Brushes.Blue; //这是最后的颜色

当我点击按钮按钮1

 私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
本。背景= f_color; //这里过渡的开始。我不希望要快。也许4秒的间隔。
}


解决方案

在代码中是可以做到的与此

 私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
ColorAnimation CA =新ColorAnimation (Colors.Red,Colors.Blue,新的持续时间(TimeSpan.FromSeconds(4)));
Storyboard.SetTarget(CA,这一点);
Storyboard.SetTargetProperty(CA,新的PropertyPath(Background.Color));

故事板STB =新的故事板();
stb.Children.Add(CA);
stb.Begin();
}



由于H.B.指出这将工作太

 私人无效的button1_Click(对象发件人,RoutedEventArgs E)
{
ColorAnimation CA =新ColorAnimation(Colors.Blue,新的持续时间(TimeSpan.FromSeconds(4)));
this.Background =新的SolidColorBrush(Colors.Red);
this.Background.BeginAnimation(SolidColorBrush.ColorProperty,CA);
}


I want to make a color transition of Background color of a WPF window.

How can I do this?

For example:

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

When I click on Button button1

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

解决方案

In code it can be done with this

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

As H.B. pointed out this will work too

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

这篇关于在WPF的色彩过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆