简体   繁体   中英

WPF - ListBox async binding to Items with animation

So.. I Have TreeView binded to ObservableCollection. TreeView is quite large so updating takes some time, but it works fine. So.. I decided to use IsAsync=True and show some loading animation while TreeView updates binding. But I get this error: "Cannot animate 'Background.Color' on an immutable object instance"

<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. But apperantly it can't be initialized from other thread. If I cut all animations in LevelTreeItemControl.xaml it works fine with 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. But those animations binds to properies like IsSelected through RelativeSource. So when TreeView updated bindings in different thread (with IsAsync=true), Items cant bind to TreeViewItem.IsSelected property. So I put those animations in TreeViewItem style and it works great!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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