c# - TreeViewItem 无法在 BackgroundWorker 中更新

标签 c# wpf backgroundworker treeviewitem

我有这个方法来更新我的 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);}

有什么想法吗?

最佳答案

关于c# - TreeViewItem 无法在 BackgroundWorker 中更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7900624/

相关文章:

ruby-on-rails - 用delay_job轮询

c# - 两个列表对象之间的 Linq 查询

c# - 使用 razor @Html.textboxfor 将字符串表示为货币或小数

c# - 深度克隆一个对象

C# trayicon 使用 wpf

c# - Application.DoEvents() 并在某些机器上卡住

c# - 在 C# 中实现进度条

c# - Task.WaitAll 和异常

c# - 如何使用 WCHAR* 在消息框中显示来自 C# 的 C 函数

c# - WPF Canvas 绑定(bind)