async lazy ItemsSource

Topics: UI Architecture
Sep 11, 2011 at 4:50 PM

Hello Guys,

How effectively solve problem with async load records for binding ItemSource?

If I have in view

<ComboBox SelectedValue="{Binding SelectedRecord.EntityB}" ItemsSource="{Binding Records}" />

in ViewModel:

public BindableCollection<EntityA> SelectedRecord { get; set; }

public BindableCollection<Entity> Records{ get; private set; }

public IEnumerable<IResult> InitViewModel() // invoked after load view cal:Message.Attach="[Event Loaded]=[Action InitViewModel]" on UserControl 

{

//before load i have not null value in SelectedRecord.EntityB but Records are empty

yield return new AsyncResult(() =>

{

Thread.Sleep(1000)//simulate load

Records.AddRange(service.LoadData());

//after load unfortunatelly i have null in SelectedRecord.EntityB and this is error !

}

}

I need function of wpf which will wait on load ItemsSource after LoadData and doesn't clear my  SelectedRecord.EntityB

 

Tank you

Petr