0

I'm working on a WPF Application where there are multiple list views.

I get all the data necessary for all the list view at once. However, my requirement is such that, the list view that has less number of items should get loaded first and displayed to the user before the next list view starts getting loaded.

So basically, I need the ListView1 to be loaded and made visible to the user first. Then ListView2 starts getting loaded and when complete is made visible to the user. In other words, all the list views are loaded when the page is loaded 9one by one) and each one is made visible right after it is loaded.

I tried using something equivalent to DoEvents() http://www.cnblogs.com/sheva/archive/2006/08/24/485790.html But it did not work.

Just to clarify; When the page is loaded and items for lv1 are populated and lv1 is shows with the items, but lv2 and lv3 will be EMPTY. Then the items for lv2 is loaded and we should see lv1 and lv2 populated with their items. At this time lv3 should be visible and empty. Then we'll get the items for lv3 and all the list views will be populated. All of them should be visible right after the first one is loaded with its items. Can anyone suggest any way to achieve this?

Thanks.

Nihar Ranjan
  • 73
  • 1
  • 5
  • Can you clarify how you want to achieve it? Do you want all the list view's visible but some empty? Or you want to make those visible one after the other? – Bhupendra Oct 10 '11 at 08:26
  • I would prefer to keep all the list views visible and populate them one-by-one. However, second option is also OK. – Nihar Ranjan Oct 10 '11 at 09:58
  • This is possible if you load all ListViews using `Visibility=Hidden`, as they all will load in no-time and then use `KeyFrame` animation to trigger `Visibility` to `Visible` for all those elements base don previou element's `Visibility` one by one. – WPF-it Oct 10 '11 at 11:00
  • Hey Nihar, have a look at this http://www.paulstovell.com/wpf-delaybinding, he made an implementation for DelayBinding. maybe you can tweak this and make it possible to binding to your other list's after a delay. This will ensure that you don't have to manage states between your view side and view-model side also you do not need to have nested message pumps (which I think will make your code a bit complicated) – Bhupendra Oct 10 '11 at 13:43

2 Answers2

0

I would use a background thread, or the Dispatcher to load your ListBoxes in order.

So you would get all your data at once, check to see which one should load first, then launch that on another thread. When that process finishes, launch the next one. And when the 2nd one finishes, load the 3rd one.

Here's a rough example:

var list1 = new int { 1, 2, 3, 4 };
var list2 = new int { 5, 6, 7 };
var list3 = new int { 8, 9 };

// Optionally do whatever checks needed to figure out which
// list should load first

this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(delegate() 
{
    MyListBox1.ItemsSource = list1;

    this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
    new Action(delegate() 
    {
        MyListBox2.ItemsSource = list2;

        this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
        new Action(delegate() 
        {
            MyListBox3.ItemsSource = list3;
        }
    }
}
Rachel
  • 130,264
  • 66
  • 304
  • 490
  • Thanks. This approach works for me as I can see the items in listbox1 when listbox2 is being loaded. However, it could be better is I can select an item from (use in any way by firing events from) listbox1 when the second one is being loaded. – Nihar Ranjan Oct 12 '11 at 06:17
0

I used backgroundworker to get the data for the first listview. On complete of backgroundworker, I populated the first ListView. Then again got the data for second listview using backgroundworker and onComplete populated the second list view and so on.

Also used the BusyIndicator (WPFToolKitExtended) for the listview that is being loaded.

Nihar Ranjan
  • 73
  • 1
  • 5
  • Need to keep the UI stuff and getting the data stuff separate so that the backgroundworker does not get a hand on the UI. – Nihar Ranjan Nov 04 '11 at 07:11