码迷,mamicode.com
首页 > Windows程序 > 详细

WPF 的Listbox 滚动处理

时间:2018-11-23 11:20:52      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:rtt   gen   nts   选择   ret   border   idt   etc   can   

操作需求场景: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 滚动处理

标签:rtt   gen   nts   选择   ret   border   idt   etc   can   

原文地址:https://www.cnblogs.com/gisbeginner/p/10005864.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!