0

我的方法有问题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应该在控件获得某些特定状态后调用;如果是这样,我怎样才能检测到它?也许使用一些事件?最终,我只需要强制我的水平列表框在右端显示最后一项,而不是像往常一样在左端显示。也许存在其他一些方法?

4

1 回答 1

1

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

您可以使用 将优先级低于 UIDispatcher的呼叫排队,从而使 UI 有时间生成视图。ScrollIntoView()

试试这个:

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 之一,您可能需要替换Dispatcher.CurrentDispatcher为。Application.Current.Dispatcher

于 2013-07-23T12:24:10.703 回答