WPF Timer控制窗体颜色渐变

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33538554/article/details/53287338

简介:

 本文主要写到System.Windows.Forms.Timer的四个属性,利用这四个属性,去不断改变窗体的颜色

案例:

 了解 Background 的值 “#1000” ,“#”后的第一位数字由小变大表示着本颜色由浅到深
 引用:System.Windows.Forms.dll       下载:http://download.csdn.net/tag/Timer.dll

源码:

------------------View

<Windowx:Class="Demo_Mvvm.Views.WindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="WindowView" Height="300" Width="300">
    <Grid Background="{Binding Background}">
    </Grid>
</Window>

----------------ViewModel

using System;
using System.Windows;
using System.Threading;
using System.Collections.ObjectModel;
using SimpleMvvmToolkit;

namespace Demo_Mvvm.ViewModels
{
    public class WindowViewModel : ViewModelBase<WindowViewModel>
    {
        //引用Timer
        System.Windows.Forms.Timer CustomerSystemTimer;
        public WindowViewModel()
        {
            //实例Timer
            CustomerSystemTimer = new System.Windows.Forms.Timer();
            //设置时间
            CustomerSystemTimer.Interval = 1000;
            //事件的执行任务
            CustomerSystemTimer.Tick += new EventHandler(ChangeTopic);
            //开始执行
            CustomerSystemTimer.Start();
        }
        public void ChangeTopic(objectsender, EventArgs e)
        {
            //TimerStop
            CustomerSystemTimer.Stop();
            BackgroundWay();
            //TimerStart
            CustomerSystemTimer.Start();
        }
        public void BackgroundWay()
        {
            if (Background != null)
            {
                Background = (Convert.ToInt32((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString()) - 9 == 0) ?"#1090" : "" + Background.Replace((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString(),"" + (Convert.ToInt32((Background.Split('0')[0].Trim().ToString()).Split('#')[1].Trim().ToString()) + 1).ToString() +"") + "";
            }
        }
        private string  _background;
        public string  Background
        {
            get
            {
                if (_background == null)
                {
                    _background = "#1090";
                }
                return _background;
            }
            set
            {
                _background = value;
                NotifyPropertyChanged(x => x.Background);
            }
        }
    }
}

截图:







猜你喜欢

转载自blog.csdn.net/qq_33538554/article/details/53287338