WPF 的Listbox 滚动处理
Posted gisbeginner
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 的Listbox 滚动处理相关的知识,希望对你有一定的参考价值。
操作需求场景:Listbox 高150 item 高150 listbox有几十个item ,希望鼠标滚轮滚动一次listbox 能滚动到下一个item,
代码实现:
<Grid x:Name="gd"> <Grid.RowDefinitions> <RowDefinition Height="158" /> <RowDefinition /> </Grid.RowDefinitions> <Grid Grid.Row="0" Name="firstrowgrid"> <ListBox ScrollViewer.PanningRatio="1" PreviewMouseWheel="waterfalllistbox_PreviewMouseWheel" ScrollViewer.ScrollChanged="waterfalllistbox_ScrollChanged" HorizontalAlignment="Stretch" ScrollViewer.CanContentScroll="False" SelectedIndex="{Binding Selectindex,Mode=TwoWay}" ScrollViewer.IsDeferredScrollingEnabled="False" SelectionChanged="waterfalllistbox_SelectionChanged" ScrollViewer.VerticalScrollBarVisibility="Visible" VirtualizingStackPanel.VirtualizationMode="Standard" VirtualizingStackPanel.IsVirtualizing="True" ItemsSource="{Binding WaterFallPoints2}" x:Name="waterfalllistbox"> <ListBox.ItemTemplate> <DataTemplate> <Grid Height="150"> <element:PtrRenderCompatible MouseDown="PtrRenderCompatible_MouseDown" Width="{Binding Width}" Height="150" PointItemsSource="{Binding DataPoints}" Brushes="{Binding BrushInfo}"/> <TextBlock Text="{Binding StartTime}" Foreground="Blue" HorizontalAlignment="Left" VerticalAlignment="Top" FontSize="12"></TextBlock> </Grid> </DataTemplate> </ListBox.ItemTemplate> <ListBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel Orientation="Vertical"></VirtualizingStackPanel> </ItemsPanelTemplate> </ListBox.ItemsPanel> </ListBox> <Line x:Name="rline" MouseLeftButtonDown="rline_MouseLeftButtonDown" Visibility="Collapsed"></Line> <Border Background="#ED9121" HorizontalAlignment="Right" VerticalAlignment="Top"> <StackPanel Width="60" Height="50"> <TextBlock TextAlignment="Center" Foreground="White" Text="选择导联:" Margin="0,2,0,2"></TextBlock> <ComboBox ItemsSource="{Binding LeadList}" Mvc.Event="SelectionChanged" Mvc.Action="{Binding SelectLeadAction}" SelectedItem="{Binding WaterFallSelectedLead,Mode=TwoWay}"> </ComboBox> </StackPanel> </Border> </Grid>
ui界面代码如上,重点是重写 waterfalllistbox_PreviewMouseWheel 鼠标滚轮事件
private void waterfalllistbox_PreviewMouseWheel(object sender, MouseWheelEventArgs e) { e.Handled = true; ScrollViewer obj = VisualHelper.FindChildren<ScrollViewer>(waterfalllistbox).First(); Debug.WriteLine("ScrollableHeight:" + obj.ScrollableHeight); Debug.WriteLine("VerticalOffset:" + obj.VerticalOffset); Debug.WriteLine("ContentVerticalOffset:" + obj.ContentVerticalOffset); Debug.WriteLine("e.Delta:" + e.Delta); if (e.Delta < 0) { obj.PageDown();//向下滚动 } else { obj.PageUp();// } }
注意:item 的高度是150,由于listboxitem 有默认的外边距和内边距,所以listbox 所在的行的高度为158
这样的话 就能在竖向滑动的时候,可以一次滑动一个。
扩展:横向滑动的时候
ScrollViewer.PageLeft(), PageRight()
Listbox 鼠标滚动的时候默认调用的是
ScrollViewer.LineDown()各个方向的line 方法;
在键盘按住上下箭头的时候默认调用pagedown 等各个方法。
(●‘?‘●) 打赏个包子也很赞奥
以上是关于WPF 的Listbox 滚动处理的主要内容,如果未能解决你的问题,请参考以下文章
wpf 中scrollviewer 中放置个LISTBOX,当焦点在LISTBOX时候,滚动鼠标不响应Scrollviewer的鼠标滚动
WPF 中的 ListBox、VirtualizingStackPanel 和平滑滚动