0

I have a problem with a listbox.ScrollIntoView method - it does not work. Here is the code snippet:

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

I've noticed that if I call ScrollIntoView a little bit later (for instance in 500 milliseconds after the source for a binding was defined) everything works. So I suppose that ScrollIntoView should be invoked after the control will obtain some specific state; if so, how can I detect it? Maybe using some event? Eventually, I just need to enforce my horizontal listbox to show the last item in a right end, but not in a left as usual. Maybe some other approach exists?

Chris Barlow
  • 3,274
  • 4
  • 31
  • 52
vklu4itesvet
  • 375
  • 1
  • 4
  • 15
  • I have no delay when calling the `ScrollIntoView` method. Perhaps your delay is caused by the initialisation of your `VirtualizableCollection`? Can you provide workable code that features your problem that we can load into a new project and test for you? – Sheridan Jul 23 '13 at 10:20
  • There is some misunderstanding. ScrollIntoView does not work at all in my case. But if I make some delay (Thread sleep) between Thumbnails initialization and ScrollIntoView invokation - then it works – vklu4itesvet Jul 23 '13 at 10:26
  • The problem that we have @vklu4itesvet, is that for you, it does not work and for us, it does. I therefore suggest that you have something in your code that is stopping it from working. If you would like us to help you find a solution, then you will have to show us the relevant code. It could be not working for any number of reasons. – Sheridan Jul 23 '13 at 10:37

1 Answers1

1

The problem is the views that represent each of the items hasn't been created yet, so the view can't be scrolled onto the screen.

You can use the Dispatcher to queue up the ScrollIntoView() call with a lower priority than the UI, which gives the UI time to generate the views.

Try this:

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

You might need to substitute Dispatcher.CurrentDispatcher with Application.Current.Dispatcher if the CurrentDispatcher happens to be one other than the UI one.

Steve
  • 6,334
  • 4
  • 39
  • 67