事情起因

测试报告说存在滚动条不能拖动的情况,我们几个开发人员多次测试都未重现该问题。后面发现是操作系统的问题,在XP和部分Win7上会存在该问题。而在我们开发人员的机器上,包括Win7 SP1,Windows Server2008上都未出现该问题。

该问题的具体表现是拖动ScrollViewer时的滚动条不能滚动里面的内容,但是点击滚动条上下方的RepeatButton(即通常情况下的三角形按钮)却能滚动里面的内容。

本以为找到了问题,解决起来会很快。但是我们几个同事试了好久,都没找到问题。我也简单看了下,开始以为会是ScrollChanged事件响应将滚动条滚回去了,结果不是。后面就忙其他的去了。再后来,另外一个同事发现是外层ScrollViewer的IsDeferredScrollingEnabled设为了True。

下面是这个情况的一个简单示例,感兴趣的朋友可以试试。

<Window x:Class="ScrollViewerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<ScrollViewer IsDeferredScrollingEnabled="True">
<Canvas Width="1000"
Height="1000"
Background="Red">
<ScrollViewer Canvas.Left="200"
Canvas.Top="200"
Width="100"
Height="100">
<Canvas Width="500" Height="500">
<Canvas.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
<GradientStop Offset="0" Color="Black" />
<GradientStop Offset="1" Color="White" />
</LinearGradientBrush>
</Canvas.Background>
</Canvas>
</ScrollViewer>
</Canvas>
</ScrollViewer>
</Grid>
</Window>

原因探索

MSDN

先MSDN查看IsDeferredScrollingEnabled的含义。

Gets or sets a value that indicates whether the content is stationary when the user drags the Thumb of a ScrollBar.

在下面有备注:

Displaying a large number of items may cause performance issues. In this case, it might be useful to use deferred scrolling. For more information, see Optimizing Performance: Controls.

This property can be used as an instance property and an attached property.

再查看Optimizing Performance: Controls中Deferred Scrolling一节

By default, when the user drags the thumb on a scrollbar, the content view continuously updates. If scrolling is slow in your control, consider using deferred scrolling. In deferred scrolling, the content is updated only when the user releases the thumb.

To implement deferred scrolling, set the IsDeferredScrollingEnabled property to true. IsDeferredScrollingEnabled is an attached property and can be set on ScrollViewer and any control that has a ScrollViewer in its control template.

这些都是我一早就知道的,该属性是用于优化性能的。默认情况下,滚动条滚动时里面的内容会更新,在数据量较大且希望快速滚动时效率会比较低,而将该属性设为true,将会在松开滚动条时才更新内容。

源码

利用ILSpy查看ScrollViewer的源码。

查找IsDeferredScrollingEnabled的引用,发现其只在如下的函数中使用

private static void OnQueryScrollCommand(object target, CanExecuteRoutedEventArgs args)
{
args.CanExecute = true;
if (args.Command == ComponentCommands.ScrollPageUp || args.Command == ComponentCommands.ScrollPageDown)
{
ScrollViewer scrollViewer = target as ScrollViewer;
Control control = (scrollViewer != null) ? (scrollViewer.TemplatedParent as Control) : null;
if (control != null && control.HandlesScrolling)
{
args.CanExecute = false;
args.ContinueRouting = true;
args.Handled = true;
return;
}
}
else
{
if (args.Command == ScrollBar.DeferScrollToHorizontalOffsetCommand || args.Command == ScrollBar.DeferScrollToVerticalOffsetCommand)
{
ScrollViewer scrollViewer2 = target as ScrollViewer;
if (scrollViewer2 != null && !scrollViewer2.IsDeferredScrollingEnabled)
{
args.CanExecute = false;
args.Handled = true;
}
}
}
}

再查找OnQueryScrollCommand的引用,发现其只在如下的函数中使用

private static void InitializeCommands()
{
ExecutedRoutedEventHandler executedRoutedEventHandler = new ExecutedRoutedEventHandler(ScrollViewer.OnScrollCommand);
CanExecuteRoutedEventHandler canExecuteRoutedEventHandler = new CanExecuteRoutedEventHandler(ScrollViewer.OnQueryScrollCommand);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.LineLeftCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.LineRightCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.PageLeftCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.PageRightCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.LineUpCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.LineDownCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.PageUpCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.PageDownCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToLeftEndCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToRightEndCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToEndCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToHomeCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToTopCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToBottomCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToHorizontalOffsetCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.ScrollToVerticalOffsetCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.DeferScrollToHorizontalOffsetCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ScrollBar.DeferScrollToVerticalOffsetCommand, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ComponentCommands.ScrollPageUp, executedRoutedEventHandler, canExecuteRoutedEventHandler);
CommandHelpers.RegisterCommandHandler(typeof(ScrollViewer), ComponentCommands.ScrollPageDown, executedRoutedEventHandler, canExecuteRoutedEventHandler);
}

而InitializeCommands仅在ScrollViewer的静态构造函数中调用。

更多关于ScrollViewer与该问题无太大关系,留待以后补充。

猜想

里层的ScrollViewer将滚动的事件传递至上层的ScrollViewer处理,在上层ScrollViewer设置了IsDeferredScrollingEnabled的某些情况下,可能会造成底层ScrollViewer命令不可用。

补充

在这篇博文都快写完的时候,发现了Nested scrollbar and deferred scrolling bug,学习到了一些知识。

将里层的ScrollViewer的IsDeferredScrollingEnabled设为true,可解决这个问题。(由于我电脑为Win7 SP1,未测试)。

在外层ScrollViewer与里层ScrollViewer的逻辑树上的某个控件上更改命令绑定。将相关命令的CanExecute总是设为true,可解决问题(同样未测试)。

更多内容,请移步上述链接。

WPF中ScrollViewer嵌套引发滚动失灵的Bug的更多相关文章

  1. WPF的ScrollViewer鼠标的滚动

    在C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果是鼠标滚动整个S ...

  2. WPF中DataGrid垂直滚动条滚动后导致每行CheckBox选择错乱

    问题: WPF的DataGrid中出现选取或者多选以及单选的时候,出现滚动条的时候,如果发生了滚动,默认情况下就会出现已经选择的CheckBox错乱.这样的原因何在? 解决方案: 经过查阅资料,了解到 ...

  3. DEVExpress For WPF 中GridControl如何实现滚动分页(延迟查询)

    在显示大量数据时一般采用分页显示,但是最近用户需要滚动显示,那么问题来了,滚动显示要求将数据全部查询回来,这显然会导致显示速度很慢. 好在想到一种方式,就是当用户滚动鼓动条的时候再查询下面的数据.好吧 ...

  4. 解答WPF中ComboBox SelectedItem Binding不上的Bug

    正在做一个打印机列表,从中选择一个打印机(System.Printing) <ComboBox Width="150" ItemsSource="{Binding ...

  5. VS编程,WPF中两个滚动条 ScrollViewer 同步滚动的一种方法

    原文:VS编程,WPF中两个滚动条 ScrollViewer 同步滚动的一种方法 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/ar ...

  6. 在WPF中实现平滑滚动

    WPF实现滚动条还是比较方便的,只要在控件外围加上ScrollViewer即可,但美中不足的是:滚动的时候没有动画效果.在滚动的时候添加过渡动画能给我们的软件增色不少,例如Office 2013的滚动 ...

  7. 解决ScrollViewer嵌套的DataGrid、ListBox等控件的鼠标滚动事件无效

    C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个DataGrid.ListBox.Listview(控件本身有scrollviewer)的时候,我们本想要的效果 ...

  8. WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)

    原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...

  9. WPF: 实现 ScrollViewer 滚动到指定控件处

    在前端 UI 开发中,有时,我们会遇到这样的需求:在一个 ScrollViewer 中有很多内容,而我们需要实现在执行某个操作后能够定位到其中指定的控件处:这很像在 HTML 页面中点击一个链接后定位 ...

随机推荐

  1. java学习笔记—标准连接池的实现(27)

    javax.sql.DataSource. Java.sql.* DataSource 接口由驱动程序供应商实现.共有三种类型的实现: 基本实现 - 生成标准的 Connection 对象 – 一个D ...

  2. 【OCP-12c】2019年CUUG OCP 071考试题库(74题)

    74.View the exhibit and examine the structure of ORDERS and CUSTOMERS tables. ORDERS Name     Null?  ...

  3. 腾讯云服务器部署 django项目整个流程

    CentOS7下部署Django项目详细操作步骤 前记:购买腾讯云服务器,配置自选,当然新用户免费体验半个月,我选择的系统是centos7系统版本, 接下来我们来看整个配置项目流程. 部署是基于:ce ...

  4. fd - 更好的 find 命令

    欢迎关注我的公众号 spider-learn fd(https://github.com/sharkdp/fd) 是 find 命令的一个更现代的替换. 对比一下 查找名字含有某个字符的文件 OLD ...

  5. Service由浅到深——AIDL的使用方式

    前言 最近有很多朋友问我这个AIDL怎么用,也许由于是工作性质的原因,很多人都没有使用过aidl,所以和他们讲解完以后,感觉对方也是半懂不懂的,所以今天我就从浅到深的分析一下这个aidl具体是怎么用的 ...

  6. C#-一维数组——★★冒泡排序★★

    ////★★★★★冒泡排序 ; i < a - ; i++) { ; j < a; j++) { if (age[i] < age[j]) { int zhong = age[i]; ...

  7. shell-005:备份。

    #  这个案例感觉有点绕,如果是本地机器备份完全没必要.下面带颜色的写法值得我们借鉴,所以还是做下笔记吧 #!/bin/bash # 找出//目录下所有txt结尾的文件,且形成一个列表清单的文件 fi ...

  8. Haskell优雅的快排实现

    说得快速排序,基本是常用的排序当中速度最快的排序了,之前也用C和Java实现过,但是过程十分痛苦,更重要的是写完代码只记得过程却对实质的过程觉得隔了一层纱,有种说不出的感觉.刚刚看一下Haskell实 ...

  9. 使用go写一个简单的exe文件

    工作需要一个小工具给分析师用,原先打算写一个脚本的,但是呢我又不会用python,要写的话只能用java来实现(打包成可执行jar,使用java -jar 的命令来执行,当然得安装jdk).这种命令行 ...

  10. css的reset和常用的html标签的默认样式整理

    先看下常用的这些标签浏览器的默认样式有哪些: body{ margin: 8px;} hr{ border:1px inset; margin-top:.5em;margin-bottom:.5em; ...