I am really stuck trying to implement RIA Services with MVVM and a crud datagrid. I think i'm missing some key idea. All of the examples I have seen use a submit button or similar to send the data back to the client. I'd rather submit as soon as a row is added/deleted/modified. I thought of a couple of approaches:
- Have my IEnuerable Itemsource property automatically bound to my context's entitycontainer. I tried this but I cant set the context's entities from the property.
- Capture the row change events and manually tell my context that is has changed items. This doesnt feel right though in a MVVM design considering my Commodity property should reflect changes in the datagrid.
Additionally, how do I tell RIA Services what method it should be using for a particular crud operation? Does this happen on the client or server?
Note I am using the ComponentOne datagrid, though I dont think its behaviour would differ from the normal Silverlight datagrid for this simple example.
View:
<c1grid:C1DataGrid x:Name="C1Grid" AutoGenerateColumns="False"
ItemsSource="{Binding Commodities}">
<c1grid:C1DataGrid.Columns>
<c1grid:DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}"></c1grid:DataGridTextColumn>
</c1grid:C1DataGrid.Columns>
</c1grid:C1DataGrid>
ViewModel:
public class CommodityViewModel : Model
{
private CommodityContext _context;
private Commodity _selectedCommodity;
public IEnumerable<Commodity> Commodities
{
get { return _context.Commodities;}
//Setter wont work here
}
public CommodityViewModel()
{
_context = new CommodityContext();
_context.Load(_context.GetCommoditiesQuery(), (LoadOperation<Commodity> loadOperation) =>
{
RaisePropertyChanged("Commodities");
_context.Commodities.EntityAdded += Commodities_EntityAdded;
}, null );
}
private void Commodities_EntityAdded(object sender, EntityCollectionChangedEventArgs<Commodity> e)
{
//how does RIA know to use my AddCommodity method on the server side? Where do I set this?
_context.SubmitChanges();
}
}