我真的很难尝试使用MVVM和crud datagrid实现RIA服务。我想我错过了一些关键的想法。我见过的所有示例都使用提交按钮或类似命令将数据发送回客户端。一旦添加/删除/修改了一行,我宁愿提交。我想到了几种方法:
此外,我如何告诉RIA Services应该使用哪种方法进行特定的crud操作?这是否发生在客户端或服务器上?
注意我正在使用ComponentOne数据网格,虽然我不认为它的行为会与这个简单示例的普通Silverlight数据网格不同。
查看:
<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>
视图模型:
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();
}
}