S
S
Space Purr2019-05-12 16:19:38
WPF
Space Purr, 2019-05-12 16:19:38

How to implement LazyLoad on TreeView?

Hello.
Let's go right away, there is a TreeView

<TreeView ItemsSource="{Binding MyObjects}" Grid.Row="0">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}">
                    <StackPanel Orientation="Horizontal">
                        <Image Source="/MyAddIn;component\Resources\treeEl.png" Stretch="Fill" Width="12" Height="12" RenderOptions.BitmapScalingMode="HighQuality"/>
                        <TextBlock Text="  " />
                        <TextBlock Text="{Binding Description}" />
                    </StackPanel>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>

The TreeView is bound to a MyObjects collection of MyObject elements that has fields
public string Description { get; set; }
public ObservableCollection<MyObject> Children { get; set; }

The MyObjects collection lies in the ViewModel and is now completely filled recursively, but since there are a lot of objects, this takes a significant amount of time.
I want my tree to fill up gradually, i.e. when expanding a tree element, the Children collection of the object was filled with new objects.
I also tried to implement it through TreeViewItem.Expander using Blend's Interactivity/Interactions libraries, I tried to implement it through IsExpanded, IsSelected triggers, I also tried to implement it through the behavior model that I dug up in Google, but nothing worked for me either. It just doesn't catch any of it. As far as I understand, this is due to the fact that the HierarchicalDataTemplate does not work directly with the TreeViewItem or something like that (I do not have enough knowledge).
The last option that I have not tested is to use the IsChecked property of the ToggleButton, which is the Expander itself of the tree element, but this is again creating a ToggleButton style and all that.
It seems to me that this can be implemented without touching this element.
I would be happy with absolutely any working options (I'm not asking you to write a complete solution here, at least an article, a page in a book, in the best case, I hope for a piece of code, of course).
I do not exclude the option that I missed somewhere, although I double-checked each method ten times and not thoughtlessly, but poked.
Without dramatizing I will say: "pa-ma-giti."
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
cyber_roach, 2019-05-19
@SpacePurr

Than the example from MSDN did not arrange?
https://docs.microsoft.com/en-us/dotnet/framework/...
There is a moment - under certain conditions, virtualization is disabled:
- if item containers are added directly to the ItemsControl.
- if the ItemsControl object contains containers of elements of various types.
- if the attached property VirtualizingStackPanel.IsVirtualizing is set to false - if the
attached property ScrollViewer.CanContentScroll is set to
false

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question