0

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.

XAML is below.

        <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>

Icon shifted to right and horizontal scroll disappeared

Sample project source code:

  • May I ask why you don't use the TreeView control instead of trying to mimic with a ListBox which is not designed to display nested items? ListBox will make your layout too complex. –  Aug 11 '23 at 17:04

0 Answers0