第一篇博客:WPF中 ScrollViewer控件的ScrollIntoView方法

在工作中碰到了一个奇葩的问题,ScrollViewer的ScrollIntoView方法一直没用。

上网找了各种方法,几乎都是说用UpdateLayOut就可以解决了,但是我的怎么也不行。

后来找了官方的文档,对应地址是:http://msdn.microsoft.com/zh-cn/library/system.windows.controls.listbox.scrollintoview%28v=VS.96%29.aspx

里面有这么一段话:

  If the specified object is not visible, calling ScrollIntoView causes it to align at the top or bottom of the viewport. When the contents of the ItemsSource collection changes, particularly if many items are added to or removed from the collection, you may need to call UpdateLayout() prior to calling ScrollIntoView for the specified item to scroll into the viewport.

大致的意思就是:如果对象不可见,调用ScrollIntoView方法会让对象在视区的顶部或者底部对齐。当ItemSource的内容发生变化时,尤其是里面的一些元素被添加或者移除时,你需要在调用ScrollIntoView 方法之前调用UpdateLayout()方法,这样才能将对应的元素调入视区。

好吧,我的问题是第一种。。。几个Tab页面切换,ScrollViewer在第二个Tab,第一个Tab中的部分功能调用了对第二个Tab页面的ScrollViewer调用了ScrollIntoView方法,结果切换回来的时候怎么都是在顶部的。。。

当然,最后的解决方案是:在切换到第二个Tab的时候,再调用一次ScrollIntoView,那样就OK了。折磨了一天的Bug终于解决。。。

第一篇blog,希望能帮助到也为这个问题抓狂的程序员们

原文地址:https://www.cnblogs.com/lisa-lin/p/3347304.html