WPF仅将数据绑定到用户操作之间的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" });

编辑:

更清楚地是我尝试过的。。

  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.....

我犯了一个错误,就是在不了解基础知识的情况下问一个问题,因此不确定我在做什么。所以我想我现在得到了。帮助我简单的思想理解它在这里的文章。

要点是了解WPF具有主线程和一个UI线程,以及如何从一个到另一个线程以允许访问UI。

http://elegantcode.com/2009/07/03/wpf-multithreading-using-the-backgroundworker-and-reporting-the-progress-the-progress-to-the-ui/

最新更新