How do I use DomainContext.Load in my ViewModel?
- by kristian
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");
}
}
}