C# 使用虚拟化StackPanel的Items控件中的精确滚动条控件

C# 使用虚拟化StackPanel的Items控件中的精确滚动条控件,c#,scroll,uwp,itemscontrol,virtualizingstackpanel,C#,Scroll,Uwp,Itemscontrol,Virtualizingstackpanel,我有一个Itemscontrol,它使用VirtualzingStackPanel来显示一个庞大(且不断增长)的项目列表: <ItemsControl Grid.Row="1" Name="ConversationItemsControl" VirtualizingStackPanel.VirtualizationMode="Recycling"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate&g

我有一个Itemscontrol,它使用VirtualzingStackPanel来显示一个庞大(且不断增长)的项目列表:

<ItemsControl Grid.Row="1" Name="ConversationItemsControl" VirtualizingStackPanel.VirtualizationMode="Recycling">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.Template>
        <ControlTemplate TargetType="ItemsControl">
            <ScrollViewer>
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <local:Message />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
scrollviewer尝试滚动到底部,但没有完全滚动到底部-它总是在“真正的”底部之前停止一点。这在某种程度上是有意义的,因为VirtualzingStackPanels使用滚动值来确定要渲染哪些项目,但这完全是在磨磨我的齿轮,最终用户无法接受


如何滚动到“真实”底部?如果我想向下滚动到某个项目的顶部位于视口的顶部(除非“真实”底部太近),我该怎么办?

这是因为内置的ItemsControl类不支持虚拟化。您可以尝试一个列表框,它默认使用UI虚拟化

如果不希望有选择行为,只需设置:

<ListBox x:Name="lbCustom">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <ContentPresenter/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>

谢谢有一件小事:在我的自定义用户控件中,我有时会替换ItemsSource并滚动到底部,但这相当麻烦(向下滚动太远)。将ItemsSource设置为空列表并事先调用UpdateLayout可缓解此问题,我是否遗漏了一些内容?我还没有得到类似的内容,因此我不知道它看起来如何以及如何帮助您解决此问题。您的故障可能有类似的来源:d
<ListBox x:Name="lbCustom">
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <ContentPresenter/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
 lbCustom.ScrollIntoView(lbCustom.Items[lbCustom.Items.Count - 1]