使用后台工作程序更新数据网格 WPF

发布于 2024-10-15 05:25:45 字数 2456 浏览 5 评论 0原文

datagrid 未在 foreach 循环中更新。在下面的代码中,数据网格在线程完成后更新,但不在线程之间更新。因为我的 foreach 循环是另一个类。我知道我必须以某种方式实现 BackgroundWorker.OnProgressChanged 方法来更新进度,但无法弄清楚。

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 类

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 绑定类

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

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"
             }  
          }
    }
}

datagrid is not updating in foreach loop. In below code datagrid is updating once thread is finish but not between. As my foreach loop is another class. I know that somehow I have to implent BackgroundWorker.OnProgressChanged Method to update the progress but can't figure out.

XMAL FILE

<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

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

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

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"
             }  
          }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哎呦我呸! 2024-10-22 05:25:45

为了在更改类的“等级”属性时更新数据网格,它需要实现 INotifyPropertyChanged。尝试按如下方式更新您的类:

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));
   }
 }
}

注意 - 我已将属性名称从“grade”更改为“Grade”以遵循 .NET 约定。我相信您可以自己添加 status 属性的实现;-)

In order for the datagrid to update when you change the 'grade' property of your class, it needs to implement 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. I am sure you can add the implementation of the status property yourself ;-)

墨离汐 2024-10-22 05:25:45

我会寻求一个解决方案,其中 dataGrid 的 ItemsSource 是一个 ObservableCollection ,使事情变得更容易,并且我认为您可以根据您所写的情况来做到这一点。

像这样,您不必费心实现 INotifyPropertyChanged 事件处理程序

(注意:ObservableCollections 修改会更新 UI,而其他 Collection 类型则不会)。这就是 MS 建议在 dataGrid 的文档中执行的操作(尽管我无法把手放在我读到的文档中说的内容上)

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.

Like this you do not have to go to the trouble of implementing the INotifyPropertyChanged event handlers

(NB: ObservableCollections modifications update the UI while other Collection types do not). 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文