user1585542
user1585542

Reputation: 35

How to load all items in listbox instead of just the visual ones

How do I load all the items in listbox instead of just the ones showing? Basically, how do you turn off the virtualizing of a Listbox? I tried but nothing worked.

            <ListBox x:Name="listBox1" VirtualizingStackPanel.IsVirtualizing="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" Background="Black" BorderThickness="0" IsEnabled="False" ForceCursor="True">
                <ListBox.RenderTransform>
                    <TranslateTransform x:Name="listBoxTransform" />
                </ListBox.RenderTransform>
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel x:Name="wp" IsItemsHost="True" ItemHeight="244" ItemWidth="184" Width="1700">
                        </WrapPanel>                            
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
                <ItemsControl.ItemTemplate>
                    <DataTemplate DataType="{x:Type Image}" x:Name="dtName">
                        <!-- The Image binding -->
                        <Image Width="170" Height="230" Source="{Binding}" Stretch="Fill" />
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ListBox> 

Upvotes: 2

Views: 1577

Answers (3)

paparazzo
paparazzo

Reputation: 45106

<ListBox VirtualizingStackPanel.IsVirtualizing="False" 
                       ItemsSource="{Binding XPath=Team}" 
                       ItemTemplate="{DynamicResource NameDataStyle}"/>

Upvotes: 1

Hannish
Hannish

Reputation: 1532

Use this code (modified from yours)

        <ListBox x:Name="listBox1" VirtualizingStackPanel.IsVirtualizing="False"
                  ScrollViewer.HorizontalScrollBarVisibility="Auto"
                  ScrollViewer.VerticalScrollBarVisibility="Auto"
                  ScrollViewer.CanContentScroll="False"
                  Background="Black" BorderThickness="0" IsEnabled="False"
                  ForceCursor="True">
            <ListBox.RenderTransform>
                <TranslateTransform x:Name="listBoxTransform" />
            </ListBox.RenderTransform>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel x:Name="wp" IsItemsHost="True" ItemHeight="244" ItemWidth="184" Width="1700">
                    </WrapPanel>                            
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type Image}" x:Name="dtName">
                    <!-- The Image binding -->
                    <Image Width="170" Height="230" Source="{Binding}" Stretch="Fill" />
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ListBox> 

I changed the VirtualizingStackPanel.IsVirtualizing to False (as suggested in a previous answer) and I added the ScrollViewer.CanContentScroll="False", which negates the virtualization and also allows for a smooth scrolling if the items inside the ListBox are too big (instead of jumping from item to item, it goes by small steps).

Hope this solves your issue, regards.

Upvotes: 3

JerKimball
JerKimball

Reputation: 16944

You'd have to override the ItemsPanel (specifically, providing a new ItemsPanelTemplate), as that is where the VirtualizingStackPanel is specified/used.

Something like this:

<ListBox>
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
</ListBox>

Upvotes: 0

Related Questions