Timer Design in StatusBar

Timer in StatusBar

  1. we need to show local time in StatusBar. solution: 1. add textblock control 2. binding to a string. 3. we use dispatcherTimer class to update the string.
  2. DispatcherTimer class
  3. Code Example:

XAML:

    <StatusBarItem HorizontalAlignment="Left" Margin="10,0,0,0">
         <TextBlock Text="当前时间: " />
    </StatusBarItem>
    <StatusBarItem HorizontalAlignment="Left">
         <TextBlock Text="{Binding CurrentTime}" />
    </StatusBarItem>

c#:

        public string CurrentTime
        {
            get
            {
                return _currentTime;
            }
            set
            {
                _currentTime = value;
                RaisePropertyChanged("");
            }
        }
        private void UpdateTime( object sender, EventArgs e)
        {
            CurrentTime = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
        }
        timer = new DispatcherTimer();
        timer.Interval = new TimeSpan(0,0,1);
        timer.Tick += new EventHandler(UpdateTime);
        timer.Start();
posted @ 2016-05-15 07:51  kongshu  阅读(182)  评论(0编辑  收藏  举报