title: scrolling in a huge ListBox with VirtualizingPanel.ScrollUnit="Pixel"
body: <p>I have some speed issues with displaying a rather long list in a ListBox in WPF 4.5: My ListBox contains about 5000 items and the items can have different heights. Each item shows information that is retrieved through a query that is executed as soon as the item is displayed. This delayed query execution is necessary as it would take too much time to do this for all content at once. I've done the delaye queries by binding an ItemsControl to a <a href="https://github.com/StephenCleary/AsyncEx/wiki/NotifyTaskCompletion" rel="nofollow">NotifyTaskCompletion</a> object that is created when the Property in the item is first accessed.</p>
  
  <p>For example:</p>
  
  <pre><code>public class ItemViewModel
  {
      public NotifyTaskCompletion&lt;ObservableCollection&lt;Content&gt;&gt; QueryResult
      {
          get
          {                
              if(_queryTask == null)
              {
                  _queryTask = new NotifyTaskCompletion&lt;ObservableCollection&lt;Content&gt;&gt; 
                                    (Task&lt;ObservableCollection&lt;Content&gt;&gt;.Run(queryFunction));
              }
              return _queryTask;
          }
      }
  }
  
  ItemTemplate:    
  +--------------------------------------------------+
  | TextBlock with header                            |
  |                                                  |
  | ItemsControl Source={Binding QueryResult.Result} |
  +--------------------------------------------------+
  </code></pre>
  
  <p>So each time an item is displayed, it starts a thread that performs the query and the content is displayed once the query is done. The information that is retrieved through the query can be quite a lot, which means that the item in the ListBox can be bigger than the ListBox itself. This is why I need to set VirtualizingPanel.ScrollUnit="Pixel" in the ListBox to make sure that the user can see the whole item. </p>
  
  <pre><code>+-------------------------------------------+
  | ListBox, too small for big Item 1         |
  |                                           |
  | +------------------------------+          |
  | | Header 1                     |          |
  | |                              |          |
  | | Lot's of information ....... |          |
  | | Lot's of information ....... |          |
  | | Lot's of information ....... |          |
  | | Lot's of information ....... |          |
  +-| Lot's of information ....... |----------+
    | Lot's of information ....... |
    | Lot's of information ....... |
    | Lot's of information ....... |
    +------------------------------+
  
    +------------------------------+
    | Header 2                     |
    |                              |
    | Not so much information..... |
    +------------------------------+
  </code></pre>
  
  <p>However, this slows down scrolling considerably. If I drag the thumb in the scrollbar to the middle of the list there is a freeze of a few seconds after which some items get displayed without the queried information. Then, some of items display the queried information. The ListBox then seems to jump to another position, which again leads to another freeze and so on. It looks like the ListBox renders all of the items that are above the current scrolling position, which takes a very long time because of all the queries and the complex rendering of the queried data.</p>
  
  <p>If I set VirtualizingPanel.ScrollUnit="Unit", I don't have any speed issues. The ListBox only displays the items that are scrolled to and thus only the information that is queried is shown. Unfortunately, I need to use the "pixel" setting because my items are sometimes so big that I need pixel scrolling to make sure that the user can see all of it.</p>
  
  <p>I'm aware of the fact that it'd be better to have a queue for the queries to avoid starting hundreds of threads at once, but I don't think this would change anything about the fundamental problem that the ListBox renders items that I don't wont to be displayed. </p>
  
  <p>I'm not really sure how to tackle this problem. Can anyone help me out?</p>