簡體   English   中英

WPF-ListBox異步綁定到帶有動畫的項目

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

所以..我有TreeView綁定到ObservableCollection。 TreeView很大,因此更新需要一些時間,但是效果很好。 所以..我決定使用IsAsync = True並在TreeView更新綁定時顯示一些加載動畫。 但我收到此錯誤:“無法為不可變對象實例設置'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是LevelTreeItemControl.xaml的虛擬機,具有自定義樣式和出色的動畫。 但是顯然,它不能從其他線程初始化。 如果我在LevelTreeItemControl.xaml中剪切所有動畫,則可以與IsAsync一起正常工作。 如何解決?

好的,我找到了引發該異常的原因:就像我很難過一樣,TreeView的ItemSource是一個不同的UserControl,女巫使用了自定義樣式以及超棒的動畫。 但是這些動畫會通過RelativeSource綁定到IsSelected之類的屬性。 因此,當TreeView在其他線程中更新綁定(使用IsAsync = true)時,Items無法綁定到TreeViewItem.IsSelected屬性。 因此,我將這些動畫設置為TreeViewItem樣式,效果很好!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM