已停止使用stopwatched的数据绑定问题

时间:2011-01-13 22:16:42

标签: c# .net wpf binding

我正在尝试创建一个类,当它启动时它启动一个秒表,并且所有时间都将经过的时间写入一个局部变量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)); } } }

}

3 个答案:

答案 0 :(得分:4)

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

public class Employee : System.ComponentModel.INotifyPropertyChanged

请参阅http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

答案 1 :(得分:2)

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

Stopwatch没有提供此类内容;你可能最好使用Timer,以便经常更新UI。只需在开始时存储DateTime,并且只要计时器触发计算从那时到现在的偏移量。

答案 2 :(得分:0)

拥有if(true)语句有什么意义?你的意思是写(真)吗?

你只是在线程中更新一次经过的时间,然后它就存在..这就是你在表格上看到的那些