-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When the TreeListView expands too much content, scrolling to the top will shrink the content #3640
Comments
This bug can be reproduced a little bit easier by following these steps:
My theoryMy theory is that the PrepareTreeListViewItem method is getting called twice, and the actual setting of the internal void PrepareTreeListViewItem(object? item, TreeListView treeListView, int level, bool isExpanded)
{
Level = level;
TreeListView = treeListView;
//NB: This can occur as part of TreeListView.PrepareContainerForItemOverride
//Because this can trigger additional collection changes we enqueue the operation
//to occur after the current operation has completed.
//Dispatcher.BeginInvoke(() =>
//{
if (GetTemplate() is HierarchicalDataTemplate { ItemsSource: { } itemsSourceBinding })
{
SetBinding(ChildrenProperty, itemsSourceBinding);
}
IsExpanded = isExpanded;
//});
DataTemplate? GetTemplate()
{
return ContentTemplate ??
ContentTemplateSelector?.SelectTemplate(item, this) ??
ContentPresenter?.Template;
}
} |
This is "by design" but there is a compelling argument that the design may be flawed (or at least confusing). First a little background. A big reason for creating this control was to have a tree view that supports UI virtualization (the default WPF tree view does not). By default, this tree view has UI virtualization enabled specifically for that reason. As you scroll the UI elements are being recycled to keep it nice and responsive. The problem comes because the The dispatcher change above is interesting however, it does introduce so issue with data binding from multiple threads specifically if the "children" are an observable collection that is being updated dynamically when the parent is expanded. There are a couple solutions here: Second, bind the IsExpanded state to something in your item's view model. This allows your view model to maintain the state of the item, even when its UI container is recycled. <materialDesign:TreeListView >
...
<materialDesign:TreeListView.ItemContainerStyle>
<Style TargetType="materialDesign:TreeListViewItem" BasedOn="{StaticResource {x:Type materialDesign:TreeListViewItem}}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded}" />
</Style>
</materialDesign:TreeListView.ItemContainerStyle>
...
</materialDesign:TreeListView> I would also be really interested in hearing thoughts on how this could be better documented or made more obvious for people. |
Bug explanation
When using the TreeViewList control, if too much data is expanded, scrolling to the top will automatically collapse all content, and it cannot maintain the expanded state.
I thought it was my own problem, the same issue occurred during the demo attempt.
Version
5.1.0
The text was updated successfully, but these errors were encountered: