5

I have a WPF ListBox with some user cards inside.
When one of those user cards is created (in the ideal case, when the current user receives it's friend list from the network), it makes a network request for a user's information.
When I scroll in the ListBox , wait a few seconds, and scroll back, the user cards have their default state.
Upon debugging, I noticed they are unloaded and recreated.

How can I stop the ListBox from virtualizing the items like this?


Code:

            <ListBox x:Name="friend_list" Background="{x:Null}" BorderBrush="{x:Null}">
                <ListBox.ItemTemplate>
                    <DataTemplate DataType="data:User">
                        <stuff:UserCard UserID="{Binding Path=UserID}" HorizontalAlignment="Stretch" Margin="0,0,0,0" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
H.B.
  • 166,899
  • 29
  • 327
  • 400
Vercas
  • 8,931
  • 15
  • 66
  • 106
  • 2
    Also yes, the ListBox is virtual by default, and you should not disable UI virtualization. – Federico Berasategui Nov 10 '12 at 22:39
  • There aren't going to be many items in there anyway. Also, the users of this app will surely have powerful computers. A few kilobytes (or even megabytes) of RAM is nothing to them. – Vercas Nov 10 '12 at 22:49
  • I don't understand. I see the ListBox, and the ItemTemplate, but I don't know where it is getting its items from. – Federico Berasategui Nov 10 '12 at 22:52
  • Having users with good machines is no reason to implement crappy applications, because of developers doing that there are lots of WPF haters – Federico Berasategui Nov 10 '12 at 22:54
  • Try setting `VirtualizingStackPanel.VirtualizationMode` to `Recycling`. – skink Nov 10 '12 at 23:32
  • 2
    @Joulukuusi. Bad idea. There should be a ViewModel to retain the data instead, and leave UI Virtualization alone, which improves application performance. – Federico Berasategui Nov 11 '12 at 00:07
  • @HighCore, I don't see how this is a bad idea. This basically instructs the container to keep already rendered items in memory, which is not disabling the virtualization. – skink Nov 11 '12 at 00:28
  • @Joulukuusi, so if you scroll the scrollbar down to the to the bottom, and the list has 100000 items, you will keep the 100000 items in memory. Nice. – Federico Berasategui Nov 11 '12 at 00:37
  • @HighCore The items are added programatically. I might switch to a resource later. And there will NEVER be more than 250. Server limitation. @ Joulukuusi I'll try that, thanks. – Vercas Nov 11 '12 at 07:52
  • @Joulukuusi Erm... Doing that makes the items move around when scrolling, and they sometimes mix their data... I think I'll have to approach this problem from another angle. I'll try caching the user data instead. – Vercas Nov 11 '12 at 08:00
  • If you post a minimal working example, we'll try to give you concrete answers instead of guessing. @HighCore, I think you completely misunderstand point of that mode. For a short explanation, please, refer to http://stackoverflow.com/a/4301534/577167 – skink Nov 11 '12 at 09:36
  • see : http://stackoverflow.com/questions/3724593/why-setting-scrollviewer-cancontentscroll-to-false-disable-virtualization – ZSH Nov 11 '12 at 09:40
  • Just FYI if the point was lost in discussion: UI virtualization != data virtualization. Each has a different impact on performance. WPF only gives UI virtualization for free, which you can turn off; just know what it really affects when you do. – BoltClock Nov 11 '12 at 11:18

1 Answers1

8

Set VirtualizingStackPanel.IsVirtualizing to false on the ListBox or set a normal StackPanel as the ListBox.ItemsPanel.

H.B.
  • 166,899
  • 29
  • 327
  • 400