[英]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
; 如果是這樣,我如何檢測到它? 也許使用一些事件? 最終,我只需要強制執行水平列表框即可將最后一項顯示在右端,而不是像往常一樣顯示在左端。 也許存在其他方法?
問題在於代表每個項目的視圖尚未創建,因此該視圖無法滾動到屏幕上。
您可以使用Dispatcher
將ScrollIntoView()
調用排隊, 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.