ListView, ListViewItems and Virtualization
I have a problem with the UI virtualization of an ListView with hundreds of elements which items can expose an Visibility property.
Unfortunately the virtualization not recognizes the visibility correct and makes the Scrollbar smaller or bigger according to how many items are in the ViewPort (and not how many ite开发者_如何学JAVAms are non-collapsed in the ViewPort) at the scrolled position.
Is there any way to avoid this problematic without turning off virtualization?
Please see example attached:
<ListView VerticalAlignment="Stretch"
Name="ListViewControl"
HorizontalAlignment="Stretch"
ItemsSource="{Binding Movies}"
ScrollViewer.VerticalScrollBarVisibility="Visible"
SelectedItem="{Binding MovieSelected, Mode=OneWayToSource}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<EventSetter Event="MouseDoubleClick" Handler="ItemClicked" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Visibility" Value="{Binding Visibility}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Stretch">
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
The point of virtualization is that the UI can render without having to examine every item in the collection, right? But the result you want is dependent on doing just that - the scrollbar can't accurately calculate the size of the thumb without knowing how many items are visible, and in order to do that it has to look at each item. When you have virtualization turned on, the scrollbar doesn't look at every item, it just uses the number of items in the collection as an estimate.
What might work better, instead of collapsing items that you don't want to see, is to remove them from the items source. Use a CollectionView
and filter out items whose Visibility
is Visibility.Collapsed
. You still incur the cost of visiting each item in the base collection to determine if it should appear in the view, but it will probably be quite a bit faster if that's done upstream of the items in the collection actually being rendered in the UI.
精彩评论