Problem in a ViewModel

Topics: Prism v4 - Silverlight 4
Jul 12, 2012 at 6:34 PM

I finished my version of UICompositionQuicStart succesfuly, but I have still one problem to solve.

My version uses RIA Services to access Northwind database. My version of EmployeeProjectsViewModel is EmployeeOrdersViewModel which loads the Employee's Orders.

The code that throws the exception is:

public class EmployeeOrdersViewModel : INotifyPropertyChanged {

    NorthwindDomainContext _Context = new NorthwindDomainContext();

    public EmployeeOrdersViewModel() {
        if (!DesignerProperties.IsInDesignTool) {
            EntityQuery queryOrders = _Context.GetOrdersQuery();
            LoadOperation loadOpOrders = _Context.Load(queryOrders);
            loadOpOrders.Completed += new EventHandler(loadOpOrders_Completed);

    void loadOpOrders_Completed(object sender, EventArgs e) {
        if (_Context.Orders.Count != 0) {
            PagedOrders = new PagedCollectionView(_Context.Orders);

    private Employee currentEmployee;
    public Employee CurrentEmployee {
        get {
            return this.currentEmployee;
        set {
            this.currentEmployee = value;

            if (this.CurrentEmployee != null)
                // This instruction throws an exception, When Prism loads this code, it tries to set the 
                // currentEmployee before the LoapOpOrders_Completed is executed (as a response of a change of the
                // Current Employee in EmployeeListView), which leads to an exception since PagedOrders is still null.
                this.PagedOrders.Filter = o => ((Order)o).EmployeeID == this.CurrentEmployee.EmployeeID;


 I don't know how to make the it to wait untilLoadOpOrders_Completed is executed.

Jul 13, 2012 at 6:08 PM

Duplicated thread.

Original question can be found in: