开发者

Why does ListView not virtualize my user controls?

I am trying to virtualize my user controls inside a ListView like this:

 <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>

but, virtualizing dosen't work, if I use a Rectangle as items, virtualzing just work, like this:

  <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 is like this:

<UserControl x:Class="WpfApplication3.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presen开发者_开发百科tation"
             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>

I use the event CleanUpVirtualizedItem to detect if virtualizing is working or not, while scrolling the view list, the event will be triggered only using Rectangles, but not the UserControl1, any idea ?


How do you know that it works with rectangles? I really doubt it, rectangles are just extremely lightweight. If you create the items statically like that there cannot be any virtualization, the items are always going to be created. You need to set an ItemsSource and let the control create the items, for that you can set an ItemTemplate which contains the user-control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜