WPF ListBox ScrollIntoView有关问题求解
WPF ListBox ScrollIntoView问题求解
官方文档上解释ScrollIntoView方法:
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.
我的问题是:将项显示在视框内时,如何指定将项显示在视框的顶端?
另外:有人说的获取ListBox内的ScrollView,调它的ScrollToVerticalOffset()方法来滚动到指定的项,但我测下来也是以像素为单位的,如何让其以项为单位向下滚动?
------解决思路----------------------
笨方法:我不知道 你的ListBox 中的项顺序有什么要求没,如果么有,就是每scrollintoView的时候,都把要显示的项 放到你Listbox数据源的第一个位置,重新刷新listbox,并将ScrollToVerticalOffset设置成0,也就是滚动条在最上部;
------解决思路----------------------
先ScrollIntoView到列表最后一项,再ScrollIntoView到你要去的项,这样就应该在显示框的最顶了,除非是倒数那几个
不过这样效果可能有点不好,刷的厉害,你可以试试看.
官方文档上解释ScrollIntoView方法:
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.
我的问题是:将项显示在视框内时,如何指定将项显示在视框的顶端?
另外:有人说的获取ListBox内的ScrollView,调它的ScrollToVerticalOffset()方法来滚动到指定的项,但我测下来也是以像素为单位的,如何让其以项为单位向下滚动?
------解决思路----------------------
笨方法:我不知道 你的ListBox 中的项顺序有什么要求没,如果么有,就是每scrollintoView的时候,都把要显示的项 放到你Listbox数据源的第一个位置,重新刷新listbox,并将ScrollToVerticalOffset设置成0,也就是滚动条在最上部;
------解决思路----------------------
先ScrollIntoView到列表最后一项,再ScrollIntoView到你要去的项,这样就应该在显示框的最顶了,除非是倒数那几个
不过这样效果可能有点不好,刷的厉害,你可以试试看.