Self-Tracking Entities in Silverlight Follow-Up

The following most excellent original post…

http://blogs.msdn.com/b/adonet/archive/2010/05/14/self-tracking-entities-in-silverlight.aspx

…has a really nice example of Self-Tracking Entities in Silverlight.

I am posting a follow-up because I have changed it a bit by extending it, maybe filling-in some blanks, and making it suit my goals.

One really needs to read the original post (linked above) in order to understand the code sample below; so, please read that first.

The code-infront for the Silverlight form is here…

<UserControl x:Class="ProductCatalog.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White" Height="300" Width="600" DataContext="{Binding}">
        <ComboBox Height="37" HorizontalAlignment="Left" Margin="12,30,0,0" Name="CategoriesComboBox" VerticalAlignment="Top" Width="267" ItemsSource="{Binding}" DisplayMemberPath="CategoryName" SelectedValuePath="CategoryID" SelectionChanged="CategoriesComboBox_SelectionChanged" />
        <sdk:Label Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="CategorySelectedLabel" VerticalAlignment="Top" Width="267" Content="[NoCategorySelected]" />
        <sdk:Label Height="27" HorizontalAlignment="Left" Margin="13,82,0,0" Name="label2" VerticalAlignment="Top" Width="266" Content="Products" />
        <sdk:DataGrid AutoGenerateColumns="True"  Height="149" HorizontalAlignment="Left" Margin="13,100,0,0" Name="MainGrid" VerticalAlignment="Top" Width="564" ItemsSource="{Binding}" />
        <Button Content="Save" Height="23" HorizontalAlignment="Left" Margin="502,265,0,0" Name="SaveButton" VerticalAlignment="Top" Width="75" Click="SaveButton_Click" />
    </Grid>
</UserControl>

The code-behind for the Silverlight form is here…

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ProductCatalog.ServiceReference;
using ProductCatalog.Web;

namespace ProductCatalog
{
    public partial class MainPage : UserControl
    {
        private ObservableCollection<Category> _catalog;

        public MainPage()
        {
            InitializeComponent();
            CatalogServiceClient service = new CatalogServiceClient();
            service.GetCatalogCompleted += (object s, GetCatalogCompletedEventArgs args) => { this.SetCatalog(args.Result); };
            service.GetCatalogAsync();
        }

        private void SetCatalog(ObservableCollection<Category> newCatalog)
        {
            int selectedIndex = this.CategoriesComboBox.SelectedIndex;
            this._catalog = newCatalog;
            this.CategoriesComboBox.DataContext = this._catalog;
            this.EnsureCategorySelection();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CatalogServiceClient service = new CatalogServiceClient();

                service.UpdateCatalogCompleted += (object s, UpdateCatalogCompletedEventArgs args) =>
                                         {
                                             this.SetCatalog(args.Result);
                                             MessageBox.Show(DateTime.Now.ToString("o") + " -- " + "The catalog saved successfully.");
                                         };

                service.UpdateCatalogAsync(this._catalog);
            }
            catch (Exception ex)
            {
                MessageBox.Show(DateTime.Now.ToString("o") + " -- Error during SaveButton_Click -- " + Environment.NewLine +  ex.ToString());
            }
        }

        private void EnsureCategorySelection()
        {
            if ((this.CategoriesComboBox != null) &&
                (this.CategoriesComboBox.Items != null) &&
                (this.CategoriesComboBox.Items.Count > 0))
            {
                if (this.CategoriesComboBox.SelectedIndex < 0)
                {
                    this.CategoriesComboBox.SelectedIndex = -1;
                    this.CategoriesComboBox.SelectedIndex = 0;
                }
            }
            else
            {
                this.CategorySelectedLabel.Content = "[NoData]";
            }
        }

        private void CategoriesComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                this.EnsureCategorySelection();
                this.CategorySelectedLabel.Content = "SelectedCategoryID='" + this.CategoriesComboBox.SelectedValue.ToString() + "'";
                var myProductList = (this._catalog.Where(c => c.CategoryID == Convert.ToInt32(this.CategoriesComboBox.SelectedValue))).SelectMany(c => c.Products);
                this.MainGrid.DataContext = myProductList;
            }
            catch (Exception ex)
            {
                MessageBox.Show(DateTime.Now.ToString("o") + " -- Error during CategoriesComboBox_SelectionChanged -- " + Environment.NewLine + ex.ToString());
            }
        }
    }
}

HTH.

— Mark Kamoski