Custom Tree View in WPF with Disappearing Icons and Virtualization Issues.

CH Rashid Ebad 0 Reputation points
2023-08-11T18:15:24.0533333+00:00

I am working a WPF desktop application where I have implemented Tree View using ListBox control. My ListBox has two StackPanels - one for content and the other for auto-display icons. It has horizontal and vertical scroll as well. Please note there is a ScrollOffsetConverter class, which calculate and assign Right Margin to Stack Panel containing to Icon. During expanding or collapsing of List Items sometimes icons either disappears or shift to the right and horizontal scrollbar disappear. However, scrolling up or down vertically bring icons back.

If I set VirtualizingPanel.IsVirtualizing="False" to the List Box, then icon stops disappearing again on scroll / expanding collapsing. However, this solution not suitable in our case. Because we large data collection which we want to bind. I have seen VirtualizingPanel.IsVirtualizing="False" only work well with very small number of items.

Icon shifted to right and horizontal scroll disappeared.
Sample Project Source code is below:
https://github.com/rashidebad/TestProject.Wpf.V3/tree/main
TreeView_Icon_Issue

  <StackPanel
    HorizontalAlignment="Center"
    VerticalAlignment="Center">
        <ListBox
            DataContext="{Binding SimpleTreeViewModel, Mode=OneTime}"
            Style="{DynamicResource TreeStyle}"
            Width="250"
            Height="500" VirtualizingPanel.IsVirtualizing="False">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <StackPanel Orientation="Horizontal">
                            <ContentControl
                            Style="{DynamicResource TreeItemIdentStyle}"/>
                            <Label
                            Style="{DynamicResource LabelStyle}"
                            Background="Transparent"
                            Content="{Binding Caption, Mode=OneWay}"/>
                        </StackPanel>
                        <StackPanel
                        Style="{DynamicResource AutoShowStyle}">
                            <Border 
                            BorderBrush="Gray"
                            Background="LightGray"
                            BorderThickness="1">
                                <Thumb
                                Style="{StaticResource ListDraggableThumbStyle}"/>
                            </Border>
                        </StackPanel>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>	
.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,103 questions
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,853 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,408 questions
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
842 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.