【问题标题】:Silverlight DataGrid - Dirty RowsSilverlight DataGrid - 脏行
【发布时间】:2011-04-21 06:06:08
【问题描述】:

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

【问题讨论】:

    标签: silverlight datagrid


    【解决方案1】:

    好吧,如果您的 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 查询 IsModified 为 true 的项目集合(此代码假定 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;
    }
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 2011-05-15
      • 2010-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-16
      相关资源
      最近更新 更多