WPF ListBox ScrollIntoView问题求解

Alaska_Lee 2014-10-09 03:03:47
官方文档上解释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()方法来滚动到指定的项,但我测下来也是以像素为单位的,如何让其以项为单位向下滚动?
...全文
425 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
阿小步 2015-09-23
  • 打赏
  • 举报
回复
this.ListBox.ScrollIntoView(this.ListBox.Items[index]);
花婷飞扬 2014-11-25
  • 打赏
  • 举报
回复
楼主在吗?请问这个问题您最后怎么解决的?求指教!
Veary 2014-10-22
  • 打赏
  • 举报
回复
引用 9 楼 lijie19871108 的回复:
[quote=引用 7 楼 u011785544 的回复:] [quote=引用 楼主 lijie19871108 的回复:] 官方文档上解释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()方法来滚动到指定的项,但我测下来也是以像素为单位的,如何让其以项为单位向下滚动?
回答第二个问题: 用获取子级的方法获取项的高度,然后在该ScrollViewer的 PreviewMouseWheel事件中控制偏移量。(适用于所有的项高度均衡)[/quote] 每一项高度不一样[/quote] 我都在括号里面给你写了 (适用于所有的项高度均衡)
Alaska_Lee 2014-10-21
  • 打赏
  • 举报
回复
引用 7 楼 u011785544 的回复:
[quote=引用 楼主 lijie19871108 的回复:] 官方文档上解释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()方法来滚动到指定的项,但我测下来也是以像素为单位的,如何让其以项为单位向下滚动?
回答第二个问题: 用获取子级的方法获取项的高度,然后在该ScrollViewer的 PreviewMouseWheel事件中控制偏移量。(适用于所有的项高度均衡)[/quote] 每一项高度不一样
Alaska_Lee 2014-10-21
  • 打赏
  • 举报
回复
引用 6 楼 kenei 的回复:
先ScrollIntoView到列表最后一项,再ScrollIntoView到你要去的项,这样就应该在显示框的最顶了,除非是倒数那几个 不过这样效果可能有点不好,刷的厉害,你可以试试看.
这个方法不错,数据不太多,效果还行!
Veary 2014-10-14
  • 打赏
  • 举报
回复
引用 楼主 lijie19871108 的回复:
官方文档上解释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()方法来滚动到指定的项,但我测下来也是以像素为单位的,如何让其以项为单位向下滚动?
回答第二个问题: 用获取子级的方法获取项的高度,然后在该ScrollViewer的 PreviewMouseWheel事件中控制偏移量。(适用于所有的项高度均衡)
kenei 2014-10-13
  • 打赏
  • 举报
回复
先ScrollIntoView到列表最后一项,再ScrollIntoView到你要去的项,这样就应该在显示框的最顶了,除非是倒数那几个 不过这样效果可能有点不好,刷的厉害,你可以试试看.
Alaska_Lee 2014-10-09
  • 打赏
  • 举报
回复
类似于百度地图,点击字母定位:
Alaska_Lee 2014-10-09
  • 打赏
  • 举报
回复
引用 3 楼 duanzi_peng 的回复:
[quote=引用 2 楼 lijie19871108 的回复:] [quote=引用 1 楼 duanzi_peng 的回复:] 这个不好控制的。要显示的项 可能是中间项,而不是第一个或者最后一个。 不如滚入 视图后改变项的背景色,以此区分。
我们这边就是有这需求啊,光改背景色不满足啊。 之前用Grid+ScrollView实现,ScrollView的ScrollToVerticalOffset()方法设置偏移单位是也不是项,显示器分辨率不同时,Scroll不到我要显示的项![/quote] 笨方法:我不知道 你的ListBox 中的项顺序有什么要求没,如果么有,就是每scrollintoView的时候,都把要显示的项 放到你Listbox数据源的第一个位置,重新刷新listbox,并将ScrollToVerticalOffset设置成0,也就是滚动条在最上部;[/quote] ListBox有顺序要求的,而且里面的项大小都不一样!
exception92 2014-10-09
  • 打赏
  • 举报
回复
引用 2 楼 lijie19871108 的回复:
[quote=引用 1 楼 duanzi_peng 的回复:] 这个不好控制的。要显示的项 可能是中间项,而不是第一个或者最后一个。 不如滚入 视图后改变项的背景色,以此区分。
我们这边就是有这需求啊,光改背景色不满足啊。 之前用Grid+ScrollView实现,ScrollView的ScrollToVerticalOffset()方法设置偏移单位是也不是项,显示器分辨率不同时,Scroll不到我要显示的项![/quote] 笨方法:我不知道 你的ListBox 中的项顺序有什么要求没,如果么有,就是每scrollintoView的时候,都把要显示的项 放到你Listbox数据源的第一个位置,重新刷新listbox,并将ScrollToVerticalOffset设置成0,也就是滚动条在最上部;
Alaska_Lee 2014-10-09
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
这个不好控制的。要显示的项 可能是中间项,而不是第一个或者最后一个。 不如滚入 视图后改变项的背景色,以此区分。
我们这边就是有这需求啊,光改背景色不满足啊。 之前用Grid+ScrollView实现,ScrollView的ScrollToVerticalOffset()方法设置偏移单位是也不是项,显示器分辨率不同时,Scroll不到我要显示的项!
exception92 2014-10-09
  • 打赏
  • 举报
回复
这个不好控制的。要显示的项 可能是中间项,而不是第一个或者最后一个。 不如滚入 视图后改变项的背景色,以此区分。

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧