用户可以执行操作,我将数据添加到绑定到DataGrid的集合,并且工作正常。
但是当有一个长时间运行的进程有多个记录添加到集合时,我从未看到DataGrid更新,直到进程结束。我究竟做错了什么?我真的很陌生,所以我确信问题很简单。
private ObservableCollection<StatusEntry> _collSe = new ObservableCollection<StatusEntry>();
public ObservableCollection<StatusEntry> CollSe
{
get { return _collSe; }
set
{
_collSe = value;
// NotifyPropertyChanged("CollSe");
}
}
CollSe.Add(new StatusEntry() { TimeStamp = DateTime.Now, Comment = "STARTED: Translating file to DataTable" });
DataTable dt = ExcelHelper.ReadAsDataTable(tbFileName.Text);
CollSe.Add(new StatusEntry() { TimeStamp = DateTime.Now, Comment = "COMPLETE: Translating file to DataTable" });
编辑:
这里更清楚的是我尝试过的东西..但是直到最后仍然没有更新UI
private void btnProcessFile_Click(object sender, RoutedEventArgs e)
{
//THis should happen as soon as the button is pressed
ThreadStart job = new ThreadStart(() =>
{
for (int i = 0; i < 20; i++)
{
// The new thread puts UI operations in the dispatching queue
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
CollSe.Add(new StatusEntry() { TimeStamp = DateTime.Now, Comment = "Happy Tools STARTED" });
}));
}
});
Thread thread = new Thread(job);
thread.Start();
//The another minute of processing here.....
答案 0 :(得分:0)
我错误地在不理解基础知识的情况下提出问题,因此在整个地方进行编码并不确定我在做什么。所以我想我现在明白了。帮助我简单的头脑理解它的文章就在这里..
重点是要了解WPF有一个主线程和一个UI线程,以及如何从一个线程转到另一个线程以允许访问UI。