在wpf中随时间间隔改变颜色



我有4个堆栈面板,和一个文本框,用户可以输入时间和一个按钮。我怎么能确保4个盒子的颜色改变(一些随机的)与给定输入的时间差距点击开始按钮。所以就像,如果输入是1秒,第一个盒子变成黄色,然后在15秒后第二个盒子变成红色,以此类推,直到第四个盒子。有什么更高效、更快捷的方法吗?

这可能就是你要找的:

Xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <StackPanel Background="{Binding Brush1}" Grid.Row="0"/>
    <StackPanel Background="{Binding Brush2}" Grid.Row="1"/>
    <StackPanel Background="{Binding Brush3}" Grid.Row="2"/>
    <StackPanel Background="{Binding Brush4}" Grid.Row="3"/>
    <TextBox Grid.Row="4" Text="{Binding Number}"/>
    <Button Grid.Row="5" Content="Start" Click="Button_Click"/>
</Grid>

背后代码(INotifyPropertyChanged实现):

    public DispatcherTimer DispatcherTimer { get; set; }
    public int Number { get; set; }
    private int _counter;
    Brush _b1;
    public Brush Brush1
    {
        get
        {
            return _b1;
        }
        set
        {
            _b1 = value;
            OnPropertyChanged("Brush1");
        }
    }
    Brush _b2;
    public Brush Brush2
    {
        get
        {
            return _b2;
        }
        set
        {
            _b2 = value;
            OnPropertyChanged("Brush2");
        }
    }
    Brush _b3;
    public Brush Brush3
    {
        get
        {
            return _b3;
        }
        set
        {
            _b3 = value;
            OnPropertyChanged("Brush3");
        }
    }
    Brush _b4;
    public Brush Brush4
    {
        get
        {
            return _b4;
        }
        set
        {
            _b4 = value;
            OnPropertyChanged("Brush4");
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        DispatcherTimer = new System.Windows.Threading.DispatcherTimer();
        DispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
        Number = 1;
    }
    private void dispatcherTimer_Tick(object sender, EventArgs e)
    {
        switch (_counter)
        {
            case 2:
                Brush2 = PickRandomBrush();
                _counter++;
                break;
            case 3:
                Brush3 = PickRandomBrush();
                _counter++;
                break;
            case 4:
                Brush4 = PickRandomBrush();
                DispatcherTimer.Stop();
                break;
            default:
                break;
        }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Brush1 = PickRandomBrush();
        _counter = 2;
        DispatcherTimer.Interval = new TimeSpan(0, 0, Number);
        DispatcherTimer.Start();
    }
    private Brush PickRandomBrush()
    {
        Brush result = Brushes.Transparent;
        Random rnd = new Random();
        Type brushesType = typeof(Brushes);
        PropertyInfo[] properties = brushesType.GetProperties();
        int random = rnd.Next(properties.Length);
        result = (Brush)properties[random].GetValue(null, null);
        return result;
    }

最新更新