我有这个方法来更新我的 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);}
有什么想法吗?
最佳答案
使用调用方法 http://msdn.microsoft.com/en-us/library/zyzhdc6b.aspx ( How to update the GUI from another thread in C#? ),这是一个如何 http://www.codeproject.com/KB/tree/ThreadingTreeView.aspx 的例子.
关于c# - TreeViewItem 无法在 BackgroundWorker 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900624/