ListBox无法向下滚动到长ListBoxItem

时间:2016-12-21 09:33:55

标签: c# wpf xaml listbox

我正在使用WPF,我遇到问题,如果ListBoxItem非常长,ListBox无法滚动到ListBoxItem的末尾。以下是代码:

<Grid>
    <ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBoxItem>
            <TextBlock Text="abc"/>
        </ListBoxItem>
        <ListBoxItem>
            <TextBlock Text="VerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylong123" TextWrapping="Wrap"/>
        </ListBoxItem>
    </ListBox>
</Grid>

请注意,我将123放在很长的文本的末尾,以查看ListBox是否可以向下滚动以查看文本。但是,这是我向下滚动时看到的内容: enter image description here

我该怎么做才能让它滚得更低(看TextBlock末尾的“123”)

此外,我希望鼠标滚轮能够在指向ListBoxItem / Text时滚动。

3 个答案:

答案 0 :(得分:1)

你可以尝试,

ScrollViewer.CanContentScroll="False"

但是由于虚拟化的成本, 将CanContentScroll设置为false将禁用ListBox的虚拟化,这可能会导致性能问题。 link - 由Alex

答案 1 :(得分:0)

您只需将ListBox放在ScrollViewer

<ScrollViewer>
    <ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBoxItem>
            <TextBlock Text="abc"/>
        </ListBoxItem>
        <ListBoxItem>
            <TextBlock Text="VerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylong123" TextWrapping="Wrap"/>
        </ListBoxItem>
    </ListBox>
</ScrollViewer>

修改:如果您想要滚动鼠标滚轮,请将PreviewMouseWheel事件添加到ScrollViewer,如下所示:

<ScrollViewer PreviewMouseWheel="UIElement_OnPreviewMouseWheel">

private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
    ScrollViewer scv = (ScrollViewer)sender;
    scv.ScrollToVerticalOffset(scv.VerticalOffset - e.Delta);
    e.Handled = true;
}

答案 2 :(得分:-2)

ListBox包裹在ScrollViewer

<Grid>
    <ScrollViewer VerticalScrollBarVisibility="Auto">
        <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBoxItem>
                <TextBlock Text="abc"/>
            </ListBoxItem>
            <ListBoxItem>
                <TextBlock Text="VerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylongVerylong123" TextWrapping="Wrap"/>
            </ListBoxItem>
        </ListBox>
    </ScrollViewer>
</Grid>