4

I have a pivot control where its item contains a listbox with items. When I scroll to the next pivot item the data binding takes some time, and I want to know when the data binding is complete because I need to enable the menu bar, as soon as the listbox is ready to appear. I couldn't find an event that can help me here. I tried the Loaded event of the listbox but while it works for some pivot items, for some others it doesn't fire! I also tried the layout updated event but it is fired too many times and it can't help me.

What could i do? thank you

johnX99
  • 73
  • 1
  • 5

1 Answers1

1

To ensure good performance when quickly scrolling through pivot items, you should wait to bind the contents of a pivot item until the SelectedIndex changes. That way it won't try to bind while the user is quickly swiping between Pivot items; it will only bind when you stop on a Pivot item.

You should then set the ItemsSource property of the ListBox in your Pivot item in the LayoutUpdated event. I use the following extension method:


        public static void InvokeOnLayoutUpdated(this FrameworkElement element, Action action)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }
            else if (action == null)
            {
                throw new ArgumentNullException("action");
            }

            // Create an event handler that unhooks itself before calling the
            // action and then attach it to the LayoutUpdated event.
            EventHandler handler = null;
            handler = (s, e) =>
            {
                element.LayoutUpdated -= handler;
                action();
            };
            element.LayoutUpdated += handler;
        }

So you would then have some code that looked something like this:


pivot.InvokeOnLayoutUpdate(() =>
  {
    Dispatcher.BeginInvoke(() => 
      {
        list.ItemsSource = source;
        ApplicationBar.IsMenuEnabled = true;
      });
  });
Derek Lakin
  • 16,179
  • 36
  • 51
  • Why Dispatch.BeginInvoke, its a `LayoutUpdated` event surely that fires on the UI Thread? – AnthonyWJones Jan 17 '11 at 13:32
  • True; almost certainly not needed. I actually use another extension mthod to invoke on the UI thread if it's not actually the UI thread, but that was left out for the sake of brevity :) – Derek Lakin Jan 17 '11 at 14:59
  • it's a nice work around, but how can i stop the data binding when the selectedIndex changes? while the data binding is in progress, the pivot doesnt respond to gestures, and if I do a gesture before the data binding is complete, it is executed after the data loading. It looks like the pivot remembers my moves and takes care of them after the data binding is complete, even if i set the IsHitTestVisible to false during this. – johnX99 Jan 17 '11 at 21:10
  • If your databinding is taking that long to complete, then you must have a huge amount of data. Are you using the default template for the ListBox (which uses a VirtualizingStackPanel), or have you replaced the ItemsPanel? If there's a noticeable delay even when using the default ListBox template, then perhaps you've got too much data and you need to use some kind of paging mechanism. – Derek Lakin Jan 18 '11 at 08:16