WEB开发网
开发学院图形图像Flash Silverlight 2 - Simple Editing of Web Service ... 阅读

Silverlight 2 - Simple Editing of Web Service Data in a DataGrid

 2008-10-24 11:46:57 来源:WEB开发网   
核心提示: public partial class Page : UserControl{public Page(){InitializeComponent();this.Loaded += OnLoaded;theGrid.CommittingCellEdit += OnCommittingCel

 public partial class Page : UserControl
 {
  public Page()
  {
   InitializeComponent();
   
   this.Loaded += OnLoaded;
   theGrid.CommittingCellEdit += OnCommittingCellEdit;
   theGrid.KeyDown += OnGridKeyDown;
   deletions = new List<DataRow>();
  }
  void OnCommittingCellEdit(object sender, DataGridCellCancelEventArgs e)
  {
   DataRow dr = e.Element.DataContext as DataRow;
   if ((dr != null) && (dr.State != RowState.Inserted))
   {
    dr.State = RowState.Modified;
   }
  }
  void OnGridKeyDown(object sender, KeyEventArgs e)
  {
   if (e.Key == Key.Delete)
   {
    DataRow dr = theGrid.SelectedItem as DataRow;
    if (dr != null)
    {
     data.Remove(dr);
     if (dr.State != RowState.Inserted)
     {
      deletions.Add(dr);
     }
    }
   }
   else if (e.Key == Key.Insert)
   {
    data.Add(new DataRow()
    {
     State = RowState.Inserted
    });
   }
  }
  void OnLoaded(object sender, RoutedEventArgs e)
  {
   data = new ObservableCollection<DataRow>();
   theGrid.DataContext = data;
   theGrid.ItemsSource = data;
  }
  void OnSave(object sender, EventArgs args)
  {
   var deleted =
    deletions.ConvertAll<Person>(DataRowToPerson).ToArray();
   var updated = (from u in data
          where u.State == RowState.Modified
          select DataRowToPerson(u)).ToArray();
   var inserted = (from i in data
           where i.State == RowState.Inserted
           select DataRowToPerson(i)).ToArray();
   PeopleServiceClient proxy = new PeopleServiceClient();
   proxy.UpdatePeopleCompleted += OnUpdatesCompleted;
   proxy.UpdatePeopleAsync(inserted, updated, deleted);
  }
  void OnUpdatesCompleted(object sender, AsyncCompletedEventArgs e)
  {
   OnLoad(null, null);
  }
  static Person DataRowToPerson(DataRow row)
  {
   return (new Person()
   {
    Id = row.Id,
    FirstName = row.FirstName,
    LastName = row.LastName,
    Age = row.Age
   });
  }
  static DataRow PersonToDataRow(Person person)
  {
   return (new DataRow()
   {
    Id = person.Id,
    FirstName = person.FirstName,
    LastName = person.LastName,
    Age = person.Age
   });
  }
  void OnLoad(object sender, EventArgs args)
  {
   data.Clear();
   deletions.Clear();
   gridLoading.Visibility = Visibility.Visible;
   PeopleServiceClient proxy = new PeopleServiceClient();
   proxy.GetPeopleCompleted += OnGetPeopleCompleted;
   proxy.GetPeopleAsync();
  }
  void OnGetPeopleCompleted(object sender, GetPeopleCompletedEventArgs e)
  {
   gridLoading.Visibility = Visibility.Collapsed;
   foreach (var person in e.Result)
   {
    data.Add(PersonToDataRow(person));
   }
  }
  private ObservableCollection<DataRow> data;
  private List<DataRow> deletions;
 }

Note that at some point I expect that you'll have to use Dispatcher.Invoke to get back to the UI thread when these asynchronous calls complete but in the current beta it seems that you don't have to do that. Maybe it'd be better practise to put it in anyway to future proof the code a bit but, as I say, that code is pretty hacky anyway :-)

I've uploaded the whole project here in case it might help anybody.

上一页  1 2 3 4 

Tags:Silverlight Simple Editing

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接