TreeViewItem 无法在BackgroundWorker 中更新

发布于 2024-12-12 03:34:31 字数 1387 浏览 0 评论 0原文

我有这个方法来更新我的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 技术交流群。

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

发布评论

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

评论(2

夏の忆 2024-12-19 03:34:31

我不确定问题是否更具体于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

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