如何在模板化所选项目的同时在WPF ListBox中维护虚拟化

时间:2011-11-04 22:25:57

标签: wpf listbox virtualization controltemplate

我有一个包含1000个项目的ListBox,当选择一个特定项目时,该项目会扩展(因为我已经模板化了ListBox Selected Item),为用户提供了更多信息。

问题在于,当我点击ListBox时,ListBox不会扩展所选项目,而是扩展其他项目。

我能够通过设置ScrollViewer.CanContentScroll="False"来解决这个问题,但它消除了虚拟化,使得控制非常慢。如果我错在这里纠正我,模板ListBoxItem有效地改变项目的高度也将删除虚拟化。

这是ListBox的xaml:

 <ListBox x:Name="AllPortfoliosList" Grid.Column="0" Grid.Row="0" ClipToBounds="False" Style="{StaticResource LooklessListBox}"   
     ItemsSource="{Binding AllPortfolioInfos}" 
     ScrollViewer.CanContentScroll="False"                                            
     ItemContainerStyle="{StaticResource RosterListItem}" />

RosterListItem:

<Style x:Key="RosterListItem" TargetType="{x:Type ListBoxItem}">
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="Template" Value="{DynamicResource item_rosterDetails}" />
        <Setter Property="HorizontalContentAlignment" Value="Left" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="Cursor" Value="Hand" />        
        <Setter Property="Padding" Value="0,0,0,0" />
        <Setter Property="ClipToBounds" Value="False" />
        <Style.Triggers>
            <Trigger Property="Selector.IsSelected" Value="True">
                <Setter Property="Template" Value="{DynamicResource Item_watchDetails}" />
            </Trigger>
        </Style.Triggers>
    </Style>

Item_watchDetails:

<ControlTemplate x:Key="Item_watchDetails">
        <Grid x:Name="grid_watch_details" HorizontalAlignment="Stretch" VerticalAlignment="Top"  SnapsToDevicePixels="True">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />   
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>    
...
</ControlTemplate>

如何解决此问题,其中ListBox仍然维护虚拟化并通过滚动启用项目模板

0 个答案:

没有答案
相关问题