gpt4 book ai didi

c# - 秒表的数据绑定(bind)问题已过去

转载 作者:太空狗 更新时间:2023-10-30 00:57:37 25 4
gpt4 key购买 nike

我正在尝试创建一个类,当它启动时它会启动一个秒表,并且一直将耗时写入一个局部变量 Elapsed,我有一个数据绑定(bind)到的 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 接口(interface)。将您的类声明修改为:

public class Employee : System.ComponentModel.INotifyPropertyChanged

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

关于c# - 秒表的数据绑定(bind)问题已过去,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4685961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com