Yesterday I wrote about how to databind RadTreeView for Silverlight to ADO.NET Data Service and use Load on Demand. The simple application from yesterday was upgraded to a small back-end application for the Northwind database, that allows editing and deleting products. This time I decided to give it to one of our designers and our new Silverlight front end developer and they implemented this slick design:
NorthwindBackEnd

The application is available online:

http://demos.telerik.com/silverlight/northwindbackend/Default.aspx

The source code can be downloaded from here:

NorthwindBackend.zip

In addition to yesterday's application, NorthwindBackend has extended data source, that provides simple undo mechanism and ability to update and delete products in the database through the ADO.NET Data Service. Product updates and deletes are really simple:

public void UpdateSelectedProduct() 
{ 
    if (this.SelectedProduct != null) 
    { 
        Service.UpdateObject(this.SelectedProduct); 
        Service.SetLink(this.SelectedProduct, "Supplier", this.SelectedProduct.Supplier); 
        Service.BeginSaveChanges(null, null); 
    } 
} 

public void DeleteSelectedProduct() 
{ 
    Service.DeleteObject(this.SelectedProduct); 
    Service.BeginSaveChanges((IAsyncResult result) => 
    { 
        SelectedProduct.Category.Products.Remove(SelectedProduct); 
        SelectedProduct = null; 
    }, null); 
} 

 

Since deleting of rows in the database can be dangerous in a real-life application, I demonstrate how to implement simple confirmation, using RadWindow:

private void ButtonDelete_Click(object sender, System.Windows.RoutedEventArgs e) 
{ 
    RadWindow.Confirm("Are you sure you want to delete this product?", 
        (object window, WindowClosedEventArgs args) => 
        { 
            if (args.DialogResult.HasValue && args.DialogResult.Value) 
            { 
                this.DataSource.DeleteSelectedProduct(); 
            } 
        }); 
} 

 

The second parameter of the RadWindow.Confirm method is an event handler, that will be executed when the window closes. There we provide the code that checks whether the user clicked OK or Cancel and then proceed accordingly.

In this application I also modified the generated code for the ADO.NET Data Service in order to change the collections' types. The new thing this time is that I added partial classes that implement INotifyPropertyChanged for each entity. You can find those classes in the Data folder of the NorthwindBackend application. The INotifyPropertyChanged interface is needed because I am using TwoWay bindings to implement the editing functionality of the application without writing code-behind:

<input:RadComboBox ItemsSource="{Binding Suppliers}" SelectedItem="{Binding SelectedProduct.Supplier, Mode=TwoWay}" DisplayMemberPath="ContactName" Height="26" /> 

 

There is one more trick, that I want to show. I am using a converter to show/hide the product properties panel, depending on the SelectedProduct property of the data source:

public class VisibilityNullConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        return value == null ? Visibility.Collapsed : Visibility.Visible; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 

 

The panel is bound to the SelectedProduct property:

<Border x:Name="ProductInfoPanel" Visibility="{Binding SelectedProduct, Converter={StaticResource VisibilityNullConverter}}" Margin="330,50,110,50" CornerRadius="10,10,10,10" BorderThickness="1,1,1,1" Height="260" BorderBrush="#FFB1C69E" Background="#19FFF200"> 

 

When the SelectedProduct is null, the converter will return Visibility.Collapsed and the Border and its content will be hidden. When the SelectedProduct becomes not null, the binding will automatically update the Visibility property of the Border, thanks to the INotifyPropertyChanged interface, that is implemented in the data source.


About the Author

Valeri Hristov

Team Lead,
Platform Team

Related Posts

Comments