有关秒表的数据绑定问题已经过去

我想创建一个类,当它启动时它启动一个秒表,并且所有的时间都被写入到一个局部变量Elapsed中,我有一个数据绑定到的Listview。 但是当我使用这段代码时,Listview只显示00:00:00.00000001并且永远不会改变。 该班的代码是:

namespace project23
{
    public class ActiveEmployee
    {
        public int EmpID { get; set; }
        public string EmpName { get; set; }
        private DateTime date;
        private BackgroundWorker worker;
        public Stopwatch sw;

        public ActiveEmployee(int empID, string empName)
        {
            date = DateTime.Now;
            worker = new BackgroundWorker();
            worker.DoWork += BackgroundWork;
            worker.WorkerReportsProgress = true;

            worker.RunWorkerAsync();
        }

        private TimeSpan elapsed;
        public TimeSpan Elapsed
        {
            get { return elapsed; }
            set
            {
                elapsed = value;
                NotifyPropertyChanged("Elapsed");
            }
        }

        private void BackgroundWork(object sender, DoWorkEventArgs args)
        {
            sw = new Stopwatch();
            sw.Start();
            if(true)
            {
                Elapsed = sw.Elapsed;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}

现在它使用定时器来代替

using System;
using System.ComponentModel;
using System.Timers;

namespace Eksamen_Januar_2011 { public class ActiveEmployee : INotifyPropertyChanged { public int EmpID { get; set; } public string EmpName { get; set; } private DateTime startDate; private BackgroundWorker worker; private Timer timer;

public ActiveEmployee(int empID, string empName) { startDate = DateTime.Now; worker = new BackgroundWorker(); worker.DoWork += BackgroundWork; timer = new Timer(1000); timer.Elapsed += TimerElapsed; worker.RunWorkerAsync(); } private TimeSpan elapsed; public TimeSpan Elapsed { get { return elapsed; } set { elapsed = value; NotifyPropertyChanged("Elapsed"); } } private void BackgroundWork(object sender, DoWorkEventArgs args) { timer.Start(); } private void TimerElapsed(object sender, ElapsedEventArgs e) { Elapsed = DateTime.Now - startDate; } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } }

}


您需要实现INotifyPropertyChanged接口。 修改你的类声明为:

public class Employee : System.ComponentModel.INotifyPropertyChanged

请参阅http://msdn.microsoft.com/zh-CN/library/system.componentmodel.inotifypropertychanged.aspx。


数据绑定和观察更改依赖于更改通知事件; 通常可以*Changed (对于属性* )或INotifyPropertyChanged (尽管自定义实现是可能的)。

Stopwatch不提供这样的; 您最好使用Timer来更新UI。 只需在开始时存储DateTime ,并在计时器触发时计算从当前到现在的偏移量。


拥有if(true)语句的意义何在? 你的意思是(true)写字吗?

你只是在线程中更新一次,然后它存在..这是你在窗体上看到的

链接地址: http://www.djcxy.com/p/8133.html

上一篇: Databinding issue with stopwatched elapsed

下一篇: DateTime vs DateTimeOffset