是的,你可以使用ColorAnimation。
您還可以搜索「使用WPF教程的ColorAnimation」。
我爲你開發了一個示例應用程序。它演示隨機顏色生成,並使用DispatcherTimer類。我已經使用了TextBox,因爲它在Text更改時引發TextChangedEvent,並且IsReadOnly =「True」。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.Window6"
Title="Window6" Height="300" Width="300" Background="Black">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="106,134,0,0" VerticalAlignment="Top" Width="75" Height="50" Background="Black" Foreground="#FFFDFBFB" BorderBrush="#00EEE416" BorderThickness="0" Click="Button_Click" />
<TextBox IsReadOnly="True" x:Name="TxtLeft" HorizontalAlignment="Left" Width="30" Margin="201,154,0,0" TextWrapping="Wrap" Text="{Binding TimeLeft}" VerticalAlignment="Top" Background="Black">
<TextBox.Foreground>
<SolidColorBrush x:Name="ForeColorKey" Color="AliceBlue"/>
</TextBox.Foreground>
<TextBox.Triggers>
<EventTrigger RoutedEvent="TextBox.TextChanged">
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="ForeColorKey" Storyboard.TargetProperty="Color" To="{Binding ToColor}" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
</TextBox>
</Grid>
</Window>
代碼隱藏
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window6.xaml
/// </summary>
public partial class Window6 : Window
{
ViewModel vm = new ViewModel();
public Window6()
{
InitializeComponent();
this.DataContext = vm;
}
Random r = new Random(1);
private void Button_Click(object sender, RoutedEventArgs e)
{
vm.TimeLeft = 15;
DispatcherTimer t = new DispatcherTimer();
t.Interval = TimeSpan.FromSeconds(1);
t.Tick += t_Tick;
t.Start();
}
void t_Tick(object sender, EventArgs e)
{
--vm.TimeLeft;
byte[] colors = new byte[3];
r.NextBytes(colors);
vm.ToColor = Color.FromRgb(colors[0], colors[1], colors[2]);
}
}
public class ViewModel : DependencyObject
{
public Color ToColor
{
get { return (Color)GetValue(ToColorProperty); }
set { SetValue(ToColorProperty, value); }
}
// Using a DependencyProperty as the backing store for ToColor. This enables animation, styling, binding, etc...
public static readonly DependencyProperty ToColorProperty =
DependencyProperty.Register("ToColor", typeof(Color), typeof(ViewModel), new PropertyMetadata(Colors.Red));
public int TimeLeft
{
get { return (int)GetValue(TimeLeftProperty); }
set { SetValue(TimeLeftProperty, value); }
}
// Using a DependencyProperty as the backing store for TimeLeft. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TimeLeftProperty =
DependencyProperty.Register("TimeLeft", typeof(int), typeof(ViewModel), new PropertyMetadata(15));
}
}
具有u檢查了我的答案嗎? – AnjumSKhan