WPF ScrollViewer滚动

目录

一、ScrollViewer 逻辑滚动

二、ScrollViewer 物理滚动

1. 物理滚动,是指按照像素滚动。

2. 滚动量的控制

 三、ScrollViewer自动滚动到最后一行

四、 ScrollViewer中的DataGrid的滚轮滚动


一、ScrollViewer 逻辑滚动

逻辑滚动就是每次点击ScrollBar的Down/Up按钮,就会向下/向上滑动一个控件的距离。

使用逻辑滚动需要将:

CanContentScroll="True"

示例如下:

点击一次正好是滚动一个Button的距离。

 <Border Grid.Row="1" BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
            <ScrollViewer Name="sv1" CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
                <StackPanel Name="sp1">
                    <Button>Button 1</Button>
                    <Button>Button 2</Button>
                    <Button>Button 3</Button>
                    <Button>Button 4</Button>
                    <Button>Button 5</Button>
                    <Button>Button 6</Button>
                    <Rectangle Width="700" Height="500" Fill="Red"/>

                </StackPanel>
            </ScrollViewer>
        </Border>

二、ScrollViewer 物理滚动

1. 物理滚动,是指按照像素滚动。

CanContentScroll="False"

2. 滚动量的控制

可以通过按钮去执行 LineUp(); LineDown();实现滚动量的控制。

示例如下:

 <StackPanel Grid.Row="1">
            <Button Content="Up" Click="svLineUp"/>
            <Button Content="Down" Click="svLineDown"/>
        </StackPanel>
        <Border Grid.Row="2" BorderBrush="Black" Background="White" BorderThickness="2" Width="500" Height="500">
            <ScrollViewer Name="sv1" CanContentScroll="False" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible">
                <StackPanel Name="sp1">
                    <Button>Button 1</Button>
                    <Button>Button 2</Button>
                    <Button>Button 3</Button>
                    <Button>Button 4</Button>
                    <Button>Button 5</Button>
                    <Button>Button 6</Button>
                    <Rectangle Width="700" Height="500" Fill="Red"/>

                </StackPanel>
            </ScrollViewer>
        </Border>
  private void svLineUp(object sender, RoutedEventArgs e)
        {
            sv1.LineUp();
            sv1.LineUp();
        }
        private void svLineDown(object sender, RoutedEventArgs e)
        {
            sv1.LineDown();
            sv1.LineDown();
        }

点击之后,没有按照控件大小滚动。

 三、ScrollViewer自动滚动到最后一行

使用ScrollChanged事件触发

通过偏移量来判断。

 代码如下:

<ScrollViewer Margin="10 0 10 0" 
                          ScrollChanged="AutoScrollChanged"
                          PreviewMouseWheel="ScrollViewer_PreviewMouseWheel"
                          >
                <DataGrid AutoGenerateColumns="False" CanUserAddRows="False" HorizontalAlignment="Left"
                          x:Name="daliLogGrid"
                          VirtualizingPanel.IsVirtualizing="True">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="No." Binding="{Binding No}" Width="*"/>
                        <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="90"/>
                        <DataGridTextColumn Header="Type" Binding="{Binding Type}" Width="2*"/>
                  
                        <DataGridTextColumn Header="Time Stamp" Binding="{Binding TimeStamp}" Width="90"/>
                    </DataGrid.Columns>
                </DataGrid>
</ScrollViewer>

 bool _autoScroll = true;
        private void AutoScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            ScrollViewer scroll = sender as ScrollViewer;
            if (scroll != null)
            {
                //当数据不再增加的时候
                if (e.ExtentHeightChange == 0)
                {
                    _autoScroll = false;
                }
                //当数据增加,自动滑动
                else
                {
                    _autoScroll = e.VerticalOffset == scroll.ScrollableHeight;

                    if (_autoScroll)
                    {
                        scroll.ScrollToVerticalOffset(scroll.ExtentHeight);
                    }
                }
               

            }
        }

四、 ScrollViewer中的DataGrid的滚轮滚动

当控件内容为DataGrid的时候无法通过滚轮去滚动内容,因为datagrid本身就有ScrollViewer。不建议在ScrollViewer中嵌套DataGrid,这样会造成内存爆炸,从而导致程序崩溃。

使用PreviewMouseWheel事件触发

 代码如下:

private void ScrollViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollViewer scroll = sender as ScrollViewer;
            if (e.Delta > 0)
            {
                scroll.LineUp();
            }
            else
            {
                
                scroll.LineDown();
            }
            
        }

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值