我正在使用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
是否可以向下滚动以查看文本。但是,这是我向下滚动时看到的内容:
我该怎么做才能让它滚得更低(看TextBlock
末尾的“123”)
此外,我希望鼠标滚轮能够在指向ListBoxItem
/ Text
时滚动。
答案 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>