使用后台工作程序更新数据网格 WPF
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了在更改类的“等级”属性时更新数据网格,它需要实现 INotifyPropertyChanged。尝试按如下方式更新您的类:
注意 - 我已将属性名称从“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:
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 ;-)
我会寻求一个解决方案,其中 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)