简体   繁体   English

使用backgroundworker更新datagrid WPF

[英]Update datagrid WPF using backgroundworker

datagrid is not updating in foreach loop. datagrid在foreach循环中未更新。 In below code datagrid is updating once thread is finish but not between. 在下面的代码中,一旦线程完成,datagrid就会更新,但不会在两者之间更新。 As my foreach loop is another class. 由于我的foreach循环是另一类。 I know that somehow I have to implent BackgroundWorker.OnProgressChanged Method to update the progress but can't figure out. 我知道我必须以某种方式要求BackgroundWorker.OnProgressChanged方法更新进度,但无法弄清楚。

XMAL FILE XMAL文件

<dg:DataGrid ItemsSource="{Binding}">
                    <dg:DataGrid.Columns>
                    <dg:DataGridTextColumn Binding="{Binding grade, Mode=TwoWay, IsAsync=True}" Header="Status"/>
                    </dg:DataGrid.Columns>
                </dg:DataGrid>

GUI CLASS GUI类

public partial class GUIClass : Page
            {
                 BackgroundWorker bgWorker = new BackgroundWorker();
                 public GUIClass ()
                 {
                    InitializeComponent();
                    bgWorker.WorkerSupportsCancellation = true;
                    bgWorker.WorkerReportsProgress = true;
                 }

                 private void btnOK_Click(object sender, RoutedEventArgs e)
                {  
                     bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
                    bgWorker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
                    bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
                    bgWorker.RunWorkerAsync();
                }

                void bgWorker_DoWork(object sender, DoWorkEventArgs e)
                {
                    ClassA cls= new ClassA();
                    cls.runprocess();
                }

             void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
             {
                    dataGrid1.Items.Refresh();
             }

             void bgWorker_RunWorkerCompleted(object sender,RunWorkerCompletedEventArgs e)
              {
                   ....
                  .....
                  dataGrid1.Items.Refresh();
              }

    }

DataGrid Binding Class DataGrid绑定类

public class BindClass
{
        public bool staus{ set; get; }
        public string grade{ set; get; }
}

ClassA ClassA的

class ClassA
{
    public void runprocess()
    {
          foreach (var item in IEnumerable<BindClass> )
          {
             if(somecondition)
             {
                  // I want to update datagrid at this stage so user can see it
                   item.grade="First"
             }  
          }
    }
}

In order for the datagrid to update when you change the 'grade' property of your class, it needs to implement INotifyPropertyChanged. 为了在更改类的“ grade”属性时更新数据网格,它需要实现INotifyPropertyChanged。 Try updating your class as follows: 尝试如下更新您的课程:

public class BindClass : INotifyPropertyChanged
{
 private string _grade;

 public string Grade
 {
   get { return _grade; }
   set
   {
     if (_grade == value)
       return;

     _grade = value;
     OnPropertyChanged("Grade");
   }
 }

 public event PropertyChangedEventHandler PropertyChanged;

 protected void OnPropertyChanged(string property)
 {
   if (PropertyChanged!=null)
   {
     PropertyChanged(this, new PropertyChangedEventArgs(property));
   }
 }
}

Note - I have changed the property name from 'grade' to 'Grade' to follow .NET conventions. 注意-我已将属性名称从“ grade”更改为“ Grade”以遵循.NET约定。 I am sure you can add the implementation of the status property yourself ;-) 我确信您可以自己添加status属性的实现;-)

I'd go for a solution where the dataGrid's ItemsSource is an ObservableCollection , makes things easier, and I think you could do it in your case from what you wrote. 我将寻求一个解决方案,其中dataGrid的ItemsSource是一个ObservableCollection ,使事情变得更容易,并且我认为您可以根据自己的情况来编写。

Like this you do not have to go to the trouble of implementing the INotifyPropertyChanged event handlers 这样,您就不必麻烦实现INotifyPropertyChanged事件处理程序

(NB: ObservableCollections modifications update the UI while other Collection types do not). (注意:ObservableCollections修改会更新UI,而其他Collection类型则不会)。 This is what MS advises to do in the dataGrid's doc (though I can't put my hand on the bit of doc I read that says it) 这是MS建议在dataGrid的文档中执行的操作(尽管我不能把手放在我读过的文档上)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM