2011-12-31 73 views
3

我有一个listview绑定到一个可观察的字符串集合。这个集合被添加到非常快(时间长达30分钟)。它运行得非常缓慢,没有虚拟化,我补充说它很棒。然而,增加的扩展是有列表自动滚动至底部后,再次很慢,我有列表视图如下:具有自动滚动性能的虚拟化ListView(慢)

<ListView Background="Transparent" 
      ItemsSource="{ 
        Binding Source={ 
          StaticResource MyViewModel} 
         ,Path=MyList}" 
      VirtualizingStackPanel.IsVirtualizing="True" 
      ScrollViewer.CanContentScroll="True" 
      ScrollViewer.VerticalScrollBarVisibility="Visible"> 
     <ListView.ItemsPanel> 
      <ItemsPanelTemplate> 
       <VirtualizingStackPanel/> 
      </ItemsPanelTemplate> 
     </ListView.ItemsPanel> 
    </ListView> 

要滚动到最后,我使用了一些扩展,我发现在网上:

/// <summary> 
    /// This method will be called when the AutoScrollToEnd 
    /// property was changed 
    /// </summary> 
    /// <param name="s">The sender (the ListBox)</param> 
    /// <param name="e">Some additional information</param> 
    public static void OnAutoScrollToEndChanged(
         DependencyObject s 
         , DependencyPropertyChangedEventArgs e) 
    { 
     var listBox = s as ListBox; 
     var listBoxItems = listBox.Items; 
     var data = listBoxItems.SourceCollection as INotifyCollectionChanged; 

     var scrollToEndHandler = 
       new NotifyCollectionChangedEventHandler(
      (s1, e1) => 
      { 
       if (listBox.Items.Count > 0) 
       { 
        object lastItem = listBox.Items[ 
             listBox.Items.Count - 1]; 
        Action action =() => 
        { 
         listBoxItems.MoveCurrentTo(lastItem); 
         listBox.ScrollIntoView(lastItem); 


        }; 
        action.Invoke(); 
       } 
      }); 

     if ((bool)e.NewValue) 
      data.CollectionChanged += scrollToEndHandler; 
     else 
      data.CollectionChanged -= scrollToEndHandler; 
    } 

我不知道ScrollIntoView方法是如何工作的,但我担心,它是否定虚拟化的性能提升。另一个猜测是,要滚动到列表中的位置,它必须找到对象而不是跳到索引。

所以我的问题是:我如何有一个列表视图更新非常快,大量的条目可以滚动到底部而不会放慢一切?

+0

在Windows 8.1 ListView中有一个ScrollIntoView-Method。但是,如你所担心的那样,它速度很慢。我也会对高性能解决方案感兴趣。 – Amenti 2014-05-10 11:26:28

回答

1

使用listBox.ScrollIntoView(lastItem)更新每个项目插入/删除/修改操作的ListBox控件。

每当ListBox项目被修改时,请致电listBox.SuspendLayout(),并在插入/删除/修改项目后使用listBox.ResumeLayout()。我相信这会解决你的问题。

此外,如果您的ListBox将有很多项目;我建议使用DoubleBufferedListBox这将有助于控制更新非常顺利。