TreeViewItem 无法在BackgroundWorker 中更新
我有这个方法来更新我的TreeView
。如果我不使用 BackgroundWorker
一切正常。但如果这样做,那么我的 TreeViewItem
不会更新,但它是 DataContex 更改。这也可以正常工作: item.IsEnabled = false;
private void twSports_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
TreeLeaf leaf = item.DataContext as TreeLeaf; var bgWorker = new BackgroundWorkerOnGrid(gridPartitions);
bgWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
if (leaf.Item != null)
{
if (leaf.Item.GetType() == typeof(SportType))
{
SportType sport = leaf.Item as SportType;
args.Result = LoadSportPartitions(sport);
}
if (leaf.Item.GetType() == typeof(SportPartition))
{
SportPartition partition = leaf.Item as SportPartition;
args.Result = LoadSportPartitions(partition);
}
}
};
bgWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
List<SportPartition> partitions = args.Result as List<SportPartition>;
if (partitions != null)
{
leaf.LoadChilds(partitions.ToArray()); //it doesn't work
item.IsEnabled = false; //it works
}
(s as BackgroundWorkerOnGrid).Dispose();
};
bgWorker.RunWorkerAsync(leaf.Item);}
有什么想法吗?
I have this method to update my TreeView
. If i don't use BackgroundWorker
all works fine. But if do then my TreeViewItem
doesn't update, but it's DataContex changes. Also this works fine: item.IsEnabled = false;
private void twSports_Expanded(object sender, RoutedEventArgs e)
{
TreeViewItem item = e.OriginalSource as TreeViewItem;
TreeLeaf leaf = item.DataContext as TreeLeaf; var bgWorker = new BackgroundWorkerOnGrid(gridPartitions);
bgWorker.DoWork += delegate(object s, DoWorkEventArgs args)
{
if (leaf.Item != null)
{
if (leaf.Item.GetType() == typeof(SportType))
{
SportType sport = leaf.Item as SportType;
args.Result = LoadSportPartitions(sport);
}
if (leaf.Item.GetType() == typeof(SportPartition))
{
SportPartition partition = leaf.Item as SportPartition;
args.Result = LoadSportPartitions(partition);
}
}
};
bgWorker.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs args)
{
List<SportPartition> partitions = args.Result as List<SportPartition>;
if (partitions != null)
{
leaf.LoadChilds(partitions.ToArray()); //it doesn't work
item.IsEnabled = false; //it works
}
(s as BackgroundWorkerOnGrid).Dispose();
};
bgWorker.RunWorkerAsync(leaf.Item);}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Invoke 方法 http://msdn.microsoft.com/en-us/library /zyzhdc6b.aspx ( 如何从 C# 中的另一个线程更新 GUI? ) ,这里是一个示例 http://www.codeproject.com/KB/tree/ThreadingTreeView.aspx。
Use Invoke method http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx ( How to update the GUI from another thread in C#? ), here is an example how http://www.codeproject.com/KB/tree/ThreadingTreeView.aspx.
我不确定问题是否更具体于WPF;但如果是的话,我的建议是:
如果您使用了 DataBinding,这可能是 DataContext 解决问题,您的 DataContext 被其他值覆盖/替换。请参考以下两个链接了解 DataContext 的工作原理:
http:// /msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontext.aspx
http://msdn.microsoft.com/en-us/library/ms752347.aspx
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontextchanged.aspx
I am not sure if the problem is more WPF specific; but if it is then here are my suggestions:
If you have used DataBinding, this might be a DataContext resolving problem, your DataContext is getting overwritten/replaced by some other value. Please refer these 2 links on how DataContext works:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontext.aspx
http://msdn.microsoft.com/en-us/library/ms752347.aspx
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.datacontextchanged.aspx