Convert Silverlight to WPF and WPF DataGrid in C#

Here is an example of a conversion from a Silverlight project that reads data and loads it to a DataGrid to a WPF project. Please read my previous post which covers the creation of the Silverlight project used in this example.

Difference #1

Silverlight is built as a UserControl which is converted from your XAML to an XAP file and then embedded into an ASP.Net ASPX file for use in a web application. While the WPF project is built as a Window and will ultimately become an EXE file with DLLs to be run on a personal PC or Virtual Server (like Microsoft Azure).

Difference #2

We do not need the xmlns reference to the System.Windows.Controls.Data namespace to access the DataGrid in WPF. Since the release of the .Net 4 Framework this exists within the framework in the System.Windows.Controls.DataGrid.

Below is the code I copied directly from the Silverlight MainPage.XAML file and pasted into the WPF MainWindow.XAML file.

[sourcecode language="xml" padlinenumbers="true"]
<ScrollViewer VerticalScrollBarVisibility="Auto"
                 BorderThickness="1" Padding="1"> 
 <StackPanel Margin="20,20,20,20"> 
  <TextBlock Text="WPF DataGrid with with SubGrid"/> 
  <DataGrid x:Name="dataGrid" Height="175"
              RowDetailsVisibilityMode="VisibleWhenSelected" > 
     <StackPanel Orientation="Horizontal"> 
      <TextBlock FontSize="14" Text="Industry: " /> 
      <TextBlock FontSize="14" Text="{Binding Industry}"/> 

I placed the above code between the <Grid> tags.

Next, let’s move to the code-behind file. I opened the Silverlight MainPage.xaml.cs file and copied the Company class, then pasted it into the WPF MainWindow.xaml.cs file. This code can be in an individual cs file or fo simplicity reasons I put it into the MainWindow code-behind.

[sourcecode language="csharp"]
public class Company
    public String Name { get; set; }
    public String Size { get; set; }
    public String Address { get; set; }
    public String Industry { get; set; }
    public Boolean? IsMajor { get; set; }
    public Boolean IsBuying { get; set; }
    public Company(String name, String size, 
                   String address, String industry, 
                   Boolean? isMajor, Boolean isBuying)
        this.Name = name;
        this.Size = size;
        this.Address = address;
        this.Industry = industry;
        this.IsMajor = isMajor;
        this.IsBuying = isBuying;
    public static List<company> GetCompanyList()
        return new List<company>(new Company[5] {
            new Company("Company 1", "< 100", 
                "Company 1 Street", "Software", 
                false, true), 
            new Company("Company 2", "> 1000", 
                "Company 2 Avenue", "Automotive", 
                false, false),
            new Company("Company 3", "< 100 > 50", 
                "Company 3 Boulevard", "IT Services", 
                null, false),
            new Company("Company 4", "> 20000", 
                "Company 4 Parkway", "Transportation", 
                true, true),
            new Company("Company 5", "<10", 
                "Company 5 Place", "Entertainment", 
                true, true)

And finally, within the WPF MainWindow constructor I place the code that finds the DataGrid and populates it with the hardcoded data from the GetCompanyList() method. Most systems would use a database to retrieve and store the data. However, for simplistic reasons I hard code it in the class.

[sourcecode language="csharp"]
DataGrid dataGrid = ((DataGrid)(this.FindName("dataGrid")));
dataGrid.ItemsSource = Company.GetCompanyList();

Difference #3

It appears the DataGrid in Silverlight and WPF handle null Booleans differently. See the picture below. In WPF the null Boolean, in column IsMajor, is empty and the other values in the column are either true or false. In Silverlight the null Boolean is a checkbox with a minus sign and the rest are either checked or unchecked checkboxes. When I removed the NULL support for the Boolean in WPF, they became checkboxes.


Download the source

Leave a Comment

Your email address will not be published.