I was reading here: Microsoft Docs: TreeView and I found the property HasUnrealizedChildren that is perfect for my case as I have a TreeStructure that could be very deep and very complex. What I've not understood is how to use it in case of a databinding. In my case I have the same:
<muxc:TreeView Name="DessertTree"
SelectionMode="Multiple"
ItemsSource="{x:Bind DataSource}">
<muxc:TreeView.ItemTemplate>
<DataTemplate x:DataType="local:Item">
<muxc:TreeViewItem
ItemsSource="{x:Bind Children}"
Content="{x:Bind Name}"/>
</DataTemplate>
</muxc:TreeView.ItemTemplate>
</muxc:TreeView>
But I do not want to load all the Children, I want to load them only when expanded as explained in the example. But in order to show the Expand gliph I need to set HasUnrealizedChildren for the root items
Here is the trick:
<DataTemplate
x:Key="ItemTemplate"
x:DataType="model:MTreeViewBase"
x:DefaultBindMode="OneWay">
<winui:TreeViewItem IsExpanded="False" ItemsSource="{x:Bind Visits}" HasUnrealizedChildren="{x:Bind Loaded, Converter={StaticResource NotEnumToBooleanConverter}, ConverterParameter=Loaded, Mode=OneWay}">
<controls1:TreeViewControl Data="{x:Bind}" />
</winui:TreeViewItem>
</DataTemplate>
I've used a Converter to change HasUnrealizedChildren to false once the item is loaded