簡體   English   中英

ListBox.SelectedItems作為HashSet

[英]ListBox.SelectedItems as HashSet

取消選擇大型項目組時,ListBox出現性能問題。 我的列表框當前包含90,000多個項目。

我認為性能問題是因為SelectedItems由列表而不是HashSet表示。

重新創建ListBox功能,支持多選以及將selecteditems實現為哈希集的最簡單方法是什么。

我不需要支持selectedindex,我認為這就是為什么selecteditems是列表的原因。

您應該找到一種虛擬化ListBox的方法,以便並非所有元素都將被加載到內存中並立即添加到列表中,而是控件的布局僅包含當前可見的元素,僅在滾動時才加載其他元素,例如像網絡一樣按需加載...

該答案詳細描述了該問題: https : //stackoverflow.com/a/2784220/559144

並且此鏈接非常有幫助: 優化性能:控制

它告訴您WPF ListBox實際上確實通過desfault支持虛擬化,並且您還可以啟用以下功能:

  • 集裝箱回收
  • 延遲滾動

我所做的是創建一個管理選擇的類,將選擇存儲為HashMap。 然后,我在ListBoxItem上處理了MouseDown,並模仿了shift和ctrl選擇。

我將選擇更改為單個,並且基本上忽略了默認選擇反射。

這是我的XAML。

<ListBox Margin="2" 
 Grid.Column="0" 
 Name="WordList" 
 ItemsSource="{Binding Source={StaticResource CVS}}"
 SelectionMode="Single">
   <ListBox.ItemContainerStyle>
      <Style TargetType="{x:Type ListBoxItem}">
         <EventSetter Event="PreviewMouseDown" Handler="Clicked"/>
         <EventSetter Event="PreviewMouseMove" Handler="MouseMoved"/>
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type ListBoxItem}">
                  <TextBlock Name="Text" Text="{Binding Word}"/>
                  <ControlTemplate.Triggers>
                     <DataTrigger Binding="{Binding IsSelected}" Value="True">
                        <Setter TargetName="Text" Property="Background" Value="LightBlue"/>
                     </DataTrigger>
                  </ControlTemplate.Triggers>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </ListBox.ItemContainerStyle>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM