【问题标题】:Change colors with time gap in wpf在 wpf 中随时间间隔改变颜色
【发布时间】:2015-07-14 06:05:30
【问题描述】:

我有 4 个堆栈面板、一个用户可以输入时间的文本框和一个按钮。 如何确保 4 个框的颜色随着单击开始按钮时给定输入的时间间隔而改变(到一些随机的颜色)。 所以就像,如果输入是 1 秒,第一个框变为黄色,然后在 1 秒后第二个框将其颜色变为红色,依此类推,直到第 4 个框。 有没有什么高效快捷的方法?

【问题讨论】:

    标签: wpf


    【解决方案1】:

    这可能就是你要找的:

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

    【讨论】:

    • 这很好。因此,我将计时器更改为毫秒,将 Number 属性更改为双倍。当我输入数字 0.1 (ms) 或小于该数字时 - 在我看来,这似乎是同一时间,但我们如何确保它在您输入时真正作用于数字?
    • 您可以使用System.Diagnostics.StopWatch 记录每次颜色更改后经过的时间。您可能想查看this answer 的一个简单示例。
    猜你喜欢
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多