Building Business Application with Silverlight 2 (Beta 2)

 

Silverlight 2 (b2) provides a new way to develop and deploy business applications in the familiar .net environment. Such applications are generally written to automate business processes, provide UI to visualize/manipulate data, and have various business rules ranging from data integrity to complex conditional logic.

In next couple of posts, I am planning to introduce the basic building blocks of a data centric application (primarily using DataGrid control), which can be used to develop full fledge business application.

Caution/Disclaimer: Code is for demo purpose only. It is neither production quality nor does it use any of the best practices.

We will start with the basic data entry functionality, introduce validation and work our way to more advance concepts. Following is a rough outline:

Part 0: Getting Started with Silverlight

Part 1: Adding new item

Part 2: Deleting item

Part 3: Validation (sync)

Part 4: Validation (async, across the wire)

Part 5: Validation (refactored)

Part 6: IEditableObject and Add new item (Take 3!)

Part 7: Beyond Validation, Prevention

Silverlight Business Application Part 0: Getting Started with Silverlight

I am assuming that you have already installed Silverlight (SDK and tools) and are familiar with basic concepts. If not, please head over to http://silverlight.net/GetStarted/, install requisite software and explore various getting started tutorials (Be sure to read Scott Guthrie's 8-part blog series)

Ok, let get started!

Launch Visual Studio and create a new Silverlight application

image

Also create the corresponding web application to host and test the Silverlight application

image

Data Model:

We will use a simple data model consisting of two entities, Person and People. Person class consists of FirstName, LastName, Age and City fields and People class to represent list of persons.

image

Add a new class called Person.cs to the Silverlight project with the following code (you will also need to add using System.ComponentModel namespace directive)

public class Person : INotifyPropertyChanged {
 
    #region Constructors
    public Person() {
    }
 
    public Person(string firstName, string lastName, int age, string city) {
        this._firstName = firstName;
        this._lastName = lastName;
        this._age = age;
        this._city = city;
    }
    #endregion
 
    #region Properties
    private string _firstName;
 
    public string FirstName {
        get { return _firstName; }
        set {
            if (value == _firstName) return;
            _firstName = value;
            OnPropertyChanged("FirstName");
        }
    }
 
    private string _lastName;
 
    public string LastName {
        get { return _lastName; }
        set {
            if (value == _lastName) return;
            _lastName = value;
            OnPropertyChanged("LastName");
        }
    }
 
    private int _age;
 
    public int Age {
        get { return _age; }
        set {
            if (value == _age) return;
            _age = value;
            OnPropertyChanged("Age");
        }
    }
 
    private string _city;
 
    public string City {
        get { return _city; }
        set {
            if (value == _city) return;
            _city = value;
            OnPropertyChanged("City");
        }
    }
    #endregion
 
    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
 
    protected void OnPropertyChanged(string name) {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
    #endregion
 
}

Add another class People.cs (to the Silverlight project ) as follows (add  using System.Collections.ObjectModel namespace directive)

public class People : ObservableCollection<Person> {
    public static People GetTestData() {
        return new People() {
            new Person("Homer", "Simpson", 38, "Springfield"),
            new Person("Marge", "Simpson", 33, "Springfield"),
            new Person("Bart", "Simpson", 8, "Springfield")
        };
    }
}

Now add reference to System.Windows.Controls.Data and System.Windows.Controls.Extended to the Silverlight project.

image

DataGrid:

Open Page.xaml and switch to Xaml view to add DataGrid to Xaml.  First add xml name space reference for System.Windows.Controls.Data assembly with prefix data as

xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

Now add DataGrid xaml declaration to Grid node. Following shows the complete page.xaml

<UserControl x:Class="Silverlight.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
    Width="400" Height="300">
    <Grid x:Name="LayoutRoot" Background="White">
        <data:DataGrid 
            x:Name="peopleDataGrid"
            />
    </Grid>
</UserControl>

In code behind page.xaml.cs file, setup Loaded event and load DataGrid with test data as shown

 
public partial class Page : UserControl {
    People _data;

    public Page() {
        InitializeComponent();
        this.Loaded += new RoutedEventHandler(Page_Loaded);
    }

    void Page_Loaded(object sender, RoutedEventArgs e) {
        _data = People.GetTestData();
        this.peopleDataGrid.ItemsSource = _data;
    }
}
 

Now F5 and run the application, you should have following UI

image

Congratulation! Play around with various DataGrid features and get a feel for the UI.

Now that we have a running application, next task is to allow user to add new items, make changes and delete items. We will tackle adding new item in next post.

Technorati Tags:
Published Monday, August 25, 2008 7:07 PM by manish.dalal
Filed under: , ,

Comments

# Silverlight Business Application Part 1: Add new item - Manish Dalal's blog

Pingback from  Silverlight Business Application Part 1: Add new item - Manish Dalal's blog

# funny wallpaper &raquo; Building Business Application with Silverlight 2 (Beta 2)

Pingback from  funny wallpaper &raquo; Building Business Application with Silverlight 2 (Beta 2)

# Silverlight Business Application Part 2: Delete item

Wednesday, August 27, 2008 5:01 PM by Manish Dalal's blog

This is part two of Building Business Application with Silverlight series that showcases the basic building

# Silverlight Business Application Part 3: Validation (sync)

Thursday, August 28, 2008 11:43 AM by Manish Dalal's blog

This is part three of Building Business Application with Silverlight series that showcases the basic

# Silverlight Business Application Part 4: Validation (async)

Wednesday, September 03, 2008 12:02 PM by Manish Dalal's blog

This is part four of Building Business Application with Silverlight series that showcases the basic building

# Silverlight news for September 4, 2008

Thursday, September 04, 2008 3:50 AM by Silverlight news for September 4, 2008

Pingback from  Silverlight news for September 4, 2008

# re: Building Business Application with Silverlight 2 (Beta 2)

Thursday, September 04, 2008 9:36 AM by john mcfetridge

interesting start but I was wondering why you bothered to make the List Observable and why you derived person from INotifyPropertyChanged as the example works just as well if it does NOT. I just bind a list as follows:

public class Person

{

    .....

}

public partial class Page : UserControl

   {

       public Page()

       {

           InitializeComponent();

           List<Person> people = new List<Person>()

           {

               new Person("Homer", "Simpson", 38, "Springfield"),

               new Person("Marge", "Simpson", 33, "Springfield"),

               new Person("Bart", "Simpson", 8, "Springfield")

           };

           MyGrid.ItemsSource = people;

if you add a button handler and change a column in a DataGrid row then you will see the change reflected in the Collection

# re: Building Business Application with Silverlight 2 (Beta 2)

Friday, September 05, 2008 5:42 PM by manish.dalal

John,

You are correct that at this point we do not need INotifyPropertyChanged, but down the line as we add more functionality it starts to become very useful.

For example, if our Person class had method that calculauted age based on birthday, INotifyPropertyChanged would be required to let UI know of change in age resulting from change in birthday. In general when ever there is change to data outside of UI, INotifyPropertyChanged is used to fire change notification.

ObservableCollection is required for DataGrid to know of changes in number of underlying object (rows), for add or delete.

-Manish

# Silverlight news for September 11, 2008

Thursday, September 11, 2008 5:29 AM by Silverlight news for September 11, 2008

Pingback from  Silverlight news for September 11, 2008

# Silverlight Business Application Part 6: IEditableobject and Add new time (Take 3!)

Wednesday, September 17, 2008 11:22 AM by Manish Dalal's blog

This is part six of Building Business Application with Silverlight series that showcases the basic building

# Prevention : The first line of defense, with Attach Property Pixie dust!

Wednesday, September 24, 2008 11:30 AM by Manish Dalal's blog

In the Building Business Application with Silverlight series we have seen how to validate data in various

# Updated Samples for Silverlight 2

Thursday, October 16, 2008 1:11 PM by Manish Dalal's blog

Now that Silverlight 2 has been released, I have updated all the samples. Conversion was mostly straight

# Cross Browser Copy and Paste in DataGrid with Excel support – Part 1

Wednesday, November 12, 2008 11:21 AM by Manish Dalal's blog

Silverlight 2 is a cross browser platform(plug-in), providing developers with a familiar .net programming

# re: Building Business Application with Silverlight 2 (Beta 2)

Sunday, February 01, 2009 12:53 PM by FabricioK

Congratulations!! I'm starting study Silverlight and this article is help me.

# Silverlight ObjectDataSource

Tuesday, April 21, 2009 9:50 AM by Manish Dalal's blog

A DataSource control represents a data object that acts as a data-interface for the data bound controls

# Databinding in Silverlight | Gopi's .net Blog

Monday, August 31, 2009 9:29 AM by Databinding in Silverlight | Gopi's .net Blog

Pingback from  Databinding in Silverlight | Gopi's .net Blog

# re: Building Business Application with Silverlight 2 (Beta 2)

Thursday, October 29, 2009 3:51 AM by hisham

is that possible to create datagrid that preview text file with every character in this file add into datagrid cell

Leave a Comment

(required) 
(required) 
(optional)
(required)