简体   繁体   English

WPF-ListBox异步绑定到带有动画的项目

[英]WPF - ListBox async binding to Items with animation

So.. I Have TreeView binded to ObservableCollection. 所以..我有TreeView绑定到ObservableCollection。 TreeView is quite large so updating takes some time, but it works fine. TreeView很大,因此更新需要一些时间,但是效果很好。 So.. I decided to use IsAsync=True and show some loading animation while TreeView updates binding. 所以..我决定使用IsAsync = True并在TreeView更新绑定时显示一些加载动画。 But I get this error: "Cannot animate 'Background.Color' on an immutable object instance" 但我收到此错误:“无法为不可变对象实例设置'Background.Color'的动画”

<TreeView Grid.Row="2" x:Name="LevelObjects"
    Style="{StaticResource TreeViewStyle}"
    ItemsSource="{Binding Path=Items, IsAsync=True}" >...

private ObservableCollection<LevelTreeItemViewModel> items;
    public ObservableCollection<LevelTreeItemViewModel> Items
    {
        get 
        {
            if (items == null)
            {
                items = GetLevelObjects();
            }
            return items; 
        }
    }

LevelTreeItemViewModel is VM for LevelTreeItemControl.xaml witch have custom style with awesome animations. LevelTreeItemViewModel是LevelTreeItemControl.xaml的虚拟机,具有自定义样式和出色的动画。 But apperantly it can't be initialized from other thread. 但是显然,它不能从其他线程初始化。 If I cut all animations in LevelTreeItemControl.xaml it works fine with IsAsync. 如果我在LevelTreeItemControl.xaml中剪切所有动画,则可以与IsAsync一起正常工作。 How to fix it? 如何解决?

Ok, I found what fired the exception: Like I sad, ItemSource for TreeView is a different UserControl, witch uses custom style with awesome animation. 好的,我找到了引发该异常的原因:就像我很难过一样,TreeView的ItemSource是一个不同的UserControl,女巫使用了自定义样式以及超棒的动画。 But those animations binds to properies like IsSelected through RelativeSource. 但是这些动画会通过RelativeSource绑定到IsSelected之类的属性。 So when TreeView updated bindings in different thread (with IsAsync=true), Items cant bind to TreeViewItem.IsSelected property. 因此,当TreeView在其他线程中更新绑定(使用IsAsync = true)时,Items无法绑定到TreeViewItem.IsSelected属性。 So I put those animations in TreeViewItem style and it works great! 因此,我将这些动画设置为TreeViewItem样式,效果很好!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM