2010-10-05 64 views
0

我有一个Silverlight DataGrid,用户可以在其中更改值。我也有一个“保存”按钮。当用户点击“保存”按钮时,我只想要保存用户已更改的数据网格中的行(项目)。我怎样才能做到这一点?Silverlight DataGrid - 脏行

回答

0

好吧,如果你的DataGrid的ItemsSource属性绑定到一个名为MyClass类的集合,你可以添加一个bool属性MyClass称为IsModified。然后,在该课程的其他指定人员中,您可以将IsModified设置为true。例如:

public class MyClass 
{ 
    public bool IsModified { get; set; } 

    private string _foo; 

    public string Foo 
    { 
     get { return _foo; } 
     set 
     { 
      _foo = value; 
      IsModified = true; 
     } 
    } 
} 

然后,你可以使用LINQ查询项目的集合,其中IsModifiedtrue(此代码假定items是绑定到你的DataGrid集合):

List<MyClass> toSave = items.Where(x => x.IsModified).ToList(); 

最后,请使用任何保存方法,您必须处理每个项目在toSave

foreach (MyClass curr in toSave) 
{ 
    // Save "curr" here... 

    // Don't forget to reset IsModified 
    curr.IsModified = false; 
} 

希望这可以帮助。