2011-10-13 19 views
0

我試圖像虛擬化這樣一個ListView裏面我的用戶控件:爲什麼ListView沒有虛擬化我的用戶控件?

<ListView VirtualizingStackPanel.IsVirtualizing="True" 
         VirtualizingStackPanel.CleanUpVirtualizedItem="stackPanel1_CleanUpVirtualizedItem" 
         VirtualizingStackPanel.VirtualizationMode="Standard" 
         Height="239" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="133" >    

       <ListView.Items> 
        <me:UserControl1 Backg="Green" /> 
        <me:UserControl1 Backg="Blue" /> 
        <me:UserControl1 Backg="Black" /> 
        <me:UserControl1 Backg="Red" /> 
        <me:UserControl1 Backg="Green" /> 
        <me:UserControl1 Backg="Blue" /> 
        <me:UserControl1 Backg="Black" /> 
        <me:UserControl1 Backg="Red" /> 
        <me:UserControl1 Backg="Blue" /> 
        <me:UserControl1 Backg="Black" /> 
        <me:UserControl1 Backg="Green" /> 
        <me:UserControl1 Backg="Green" /> 
       </ListView.Items> 
</ListView> 

但是,虛擬化這麼想的工作,如果我用一個矩形作爲項virtualzing只是工作,像這樣:

<ListView VirtualizingStackPanel.IsVirtualizing="True" 
         VirtualizingStackPanel.CleanUpVirtualizedItem="stackPanel1_CleanUpVirtualizedItem" 
         VirtualizingStackPanel.VirtualizationMode="Standard" 
         Height="239" HorizontalAlignment="Left" Name="stackPanel1" VerticalAlignment="Top" Width="133" >    

       <ListView.Items> 
        <Rectangle Width="20" Height="20" Fill="Gray" ></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Green"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Orange"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Blue"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Black"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Red"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Gray"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Green"></Rectangle> 
       <Rectangle Width="20" Height="20" Fill="Orange"></Rectangle> 
       </ListView.Items> 
</ListView> 

的UserControl1 XAML是這樣的:

<UserControl x:Class="WpfApplication3.UserControl1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
      Width="20" Height="20" 
      > 
    <Grid> 
     <Rectangle Name="internalRect" Width="20" Height="20" Fill="Black"></Rectangle> 
    </Grid> 
</UserControl> 

我使用事件CleanUpVirtualizedItem如果虛擬化是工作或沒有,而滾動噸至檢測他查看列表,只會使用矩形觸發事件,但不會觸發UserControl1,任何想法?

+0

我不確定它爲什麼適用於矩形,否則我會發布這個答案。所以只是一個建議:MSDN「只有當面板中的項目控件創建了自己的項目容器時,纔會出現」StackPanel中的虛擬化「,您可以通過使用數據綁定來確保這種情況發生,在項目容器被創建並添加到項目控件,VirtualizingStackPanel與StackPanel相比,沒有性能優勢。「 – dowhilefor

回答

7

你如何知道它與矩形一起工作?我真的懷疑它,矩形非常輕巧。如果您像這樣靜態創建項目,則不會有任何虛擬化,這些項目將始終創建。您需要設置ItemsSource並讓控件創建項目,因爲您可以設置包含用戶控件的ItemTemplate

+0

+1「準確!」 –

相關問題