繁体   English   中英

ListBox.ScrollIntoView仅在延迟后才能工作

[英]ListBox.ScrollIntoView works only after delay

我有一个listbox.ScrollIntoView方法的问题-它不起作用。 这是代码片段:

// the listbox is binded to a "Thumbnails" property
this.Thumbnails = new VirtualizableCollection<RecordingThumbnailItem>(this.thumbnailsProvider) { ItemsStep = this.ThumbnailsStep };
this.listBox.ScrollIntoView(this.Thumbnails[thumbnailToSelect]);

我注意到,如果稍后再调用ScrollIntoView (例如,在定义绑定源之后的500毫秒内),一切正常。 因此,我假定在控件将获得某些特定状态之后应调用ScrollIntoView 如果是这样,我如何检测到它? 也许使用一些事件? 最终,我只需要强制执行水平列表框即可将最后一项显示在右端,而不是像往常一样显示在左端。 也许存在其他方法?

问题在于代表每个项目的视图尚未创建,因此该视图无法滚动到屏幕上。

您可以使用DispatcherScrollIntoView()调用排队, ScrollIntoView()优先级低于UI,这使UI有时间生成视图。

尝试这个:

this.Thumbnails = new VirtualizableCollection<RecordingThumbnailItem>(this.thumbnailsProvider) { ItemsStep = this.ThumbnailsStep };
Dispatcher.CurrentDispatcher.BeginInvoke(
    DispatcherPriority.ContextIdle,
    new Action(() => this.listBox.ScrollIntoView(this.Thumbnails[thumbnailToSelect])
);

如果CurrentDispatcher恰好是UI之外的一个,则可能需要用Application.Current.Dispatcher替换Dispatcher.CurrentDispatcher

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM