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 += 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.
Tags:Silverlight Simple Editing
编辑录入:爽爽 [复制链接] [打 印]- ››silverlight全屏显示图片
- ››Silverlight MVVM 模式(一) 切近实战
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››Silverlight for Symbian
- ››Silverlight3系列(四)数据绑定 Data Binding 1
- ››Simple Cloud API:编写可移植的、可互操作的云应...
- ››silverlight2 游戏 1 你能坚持多少秒
- ››Silverlight开发实践--PicZoomShow
- ››Silverlight自定义控件开发 - 令人懊恼的OnApplyT...
更多精彩
赞助商链接