Up Display code samples
DevForce 2010 Resource Center » Code samples » Display code samples » Code sample: Editing with the ObjectDataSource (Silverlight)

Code sample: Editing with the ObjectDataSource (Silverlight)

Last modified on August 15, 2012 17:22

This sample builds upon the previous samples of the DevForce ObjectDataSource (ODS) control to add simple editing capabilities.  The ObjectDataSource allows you to define and configure a data source directly in your XAML.  The ODS supports asynchronous querying, paging, grouping, sorting and filtering of an EntityQuery.


Problem

If you have a lot of data to display and need to allow users to page, group, sort and filter the data.  You prefer the simplicity of a drag and drop control.

Solution

The ObjectDataSource allows you to define and configure a data source directly in your XAML. The ODS supports asynchronous querying, paging, grouping, sorting and filtering of an EntityQuery.

This sample builds upon the previous samples, showing simple paging and grouping and filtering, by adding editing capabilities.

MainPage.xaml

The markup is similar to what we've seen in previous samples, but we've now included a simple DataForm, also bound to the ODS.

MainPage.xaml.cs

We see a little more logic in the code behind here than in previous samples.  To note is the SaveChanges and RejectChanges calls made directly on the ObjectDataSource:

C#
  _customersDataSource.SaveChanges(saveArgs => {
 if (saveArgs.Error != null) {
    MessageBox.Show(saveArgs.Error.Message);
    saveArgs.MarkErrorAsHandled();
  } else {
    MessageBox.Show("Changes saved!");
  }
});
VB
  _customersDataSource.SaveChanges(Sub(saveArgs)
 If saveArgs.Error IsNot Nothing Then
    MessageBox.Show(saveArgs.Error.Message)
    saveArgs.MarkErrorAsHandled()
 Else
    MessageBox.Show("Changes saved!")
 End If
End Sub)

Not shown in the sample, the ODS also contains a few dependency properties and visual states you can use to provide a better editing experience:

C#
public bool HasChanges { get; }
public bool IsBusy { get; }
public bool IsLoadingData { get; }
public bool IsSavingChanges { get; }

[TemplateVisualState(Name = "Enabled", GroupName = "CommonState")]
[TemplateVisualState(Name = "Disabled", GroupName = "CommonState")]
[TemplateVisualState(Name = "Idle", GroupName = "ActivityState")]
[TemplateVisualState(Name = "Loading", GroupName = "ActivityState")]
[TemplateVisualState(Name = "Saving", GroupName = "ActivityState")]
VB
Public ReadOnly Property HasChanges() As Boolean
Public ReadOnly Property IsBusy() As Boolean
Public ReadOnly Property IsLoadingData() As Boolean
Public ReadOnly Property IsSavingChanges() As Boolean

<TemplateVisualState(Name = "Enabled", GroupName = "CommonState")>
<TemplateVisualState(Name = "Disabled", GroupName = "CommonState")>
<TemplateVisualState(Name = "Idle", GroupName = "ActivityState")>
<TemplateVisualState(Name = "Loading", GroupName = "ActivityState")>
<TemplateVisualState(Name = "Saving", GroupName = "ActivityState")>

Prerequisites

The Silverlight 4 Toolkit

Created by DevForce on October 24, 2010 11:56

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2015 IdeaBlade