How do I use DomainContext.Load in my ViewModel?

Posted by kristian on Stack Overflow See other posts from Stack Overflow or by kristian
Published on 2010-06-18T00:57:12Z Indexed on 2010/06/18 1:03 UTC
Read the original article Hit count: 761

I'm trying to use RIA services to provide data to my Silverlight application by calling DomainContext.Load to retrieve a collection of widgets. I want to expose this collection through a property of the ViewModel so I can bind a control to the collection in my page.

I think my approach must be fundamentally wrong because Load is called asynchronously and is therefore not available when my page loads and the control tried to bind. Can someone please show me the right way to do this?

My Silverlight page has the following XAML:

<navigation:Page x:Class="Demo.UI.Pages.WidgetPage" 
               // the usual xmlns stuff here...
               xmlns:local="clr-namespace:Demo.UI.Pages" mc:Ignorable="d"
               xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"

                d:DataContext="{d:DesignInstance Type=local:WidgetPageModel, IsDesignTimeCreatable=False}"

               d:DesignWidth="640" d:DesignHeight="480"
               Title="Widget Page">
        <Canvas x:Name="LayoutRoot">
            <ListBox ItemsSource="{Binding RedWidgets}" Width="150" Height="500" />
        </Canvas>
    </navigation:Page>

My ViewModel looks like this:

public class WidgetPageModel
{
    private WidgetDomainContext WidgetContext { get; set; }

    public WidgetPageModel()
    {          
        this.WidgetContext = new WidgetDomainContext();

        WidgetContext.Load(WidgetContext.GetAllWidgetsQuery(), false);            

    }

    public IEnumerable<Widget> RedWidgets
    {
        get
        {
            return this.WidgetContext.Widgets.Where(w => w.Colour == "Red");
        }
    }
}

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-4.0