Introduction

When you write code in the LightSwitch client layer, you have programmatic access to most of the application. This also applies when you
use Silverlight Custom Controls. This is important because it saves you from writing a lot of code. A professional developer can be far more
productive when creating a LightSwitch application than when they are coding a Silverlight application without using LightSwitch.

In this article, we will take a random walk through of the LightSwitch API (Application Programming Interface) to give you an idea of the programmatic access available to you from
a LightSwitch Silverlight Custom Control (this also applies to LightSwitch
Control Extensions).

Note: You must have Visual Studio Professional (or higher) to create a Silverlight control using the method described in this article.

The LightSwitch Explorer Application

We will take our random walk through of the LightSwitch API by creating a sample LightSwitch application. We will create a Silverlight Custom Control called “LightSwitch Explorer” that
will display information about the collections of the screen that it is displayed on, raise methods, and switch screens.

We create a simple LightSwitch application that allows Products and Orders to be managed.

One screen for Products.

One screen for Orders.

The Silverlight Control

Add a new project to the Solution.

We add a Silverlight Class Library.

We select Silverlight 4.

The Project will show in the Solution Explorer, we right-click on the Class1.cs file and Delete it.

We add a New Item to the SilverlightControlLibrary project.

We add a Silverlight User Control, and call it LightSwitchExplorer.xaml.

We right-click on References, and select Add Reference.

We add references to:

Microsoft.LightSwitch

Microsoft.LightSwitch.Base.Client

Microsoft.LightSwitch.Client

This will allow access to the LightSwitch API in the code-behind file in the LightSwitchExplorer.xaml control.

We open the LightSwitchExplorer.xaml.cs file, and add the following using statements:

using Microsoft.LightSwitch;

using Microsoft.LightSwitch.Client;

using Microsoft.LightSwitch.Details;

using Microsoft.LightSwitch.Details.Client;

using Microsoft.LightSwitch.Model;

using Microsoft.LightSwitch.Presentation;

using System.Windows.Data;

using System.Collections;

We open the LightSwitchExplorer.xaml file, and change the markup to the following:

This will automatically add the needed assemblies to support the DataGrid.

We click and drag the corners of the grid to size it so it fill the entire bottom half of the page.

In the Properties for the DataGrid, we check the box next to AutoGenerateColumns.

We drag and drop a Label above the first box and change its Content to “Active Screens”.

We add two more Labels and name them Methods and Collections.

Add Code to the Silverlight Control

Open the LightSwitchExplorer.xaml.cs file, and add the following code:

// This property is created because it allows us to raise
// "OnExplorerControlDataContextPropertyChanged"
// When the DataContext is updated
publicstaticreadonly DependencyProperty ExplorerControlDataContextProperty =
DependencyProperty.Register("DummyProperty", typeof(IContentItem),
typeof(LightSwitchExplorer),
new PropertyMetadata(OnExplorerControlDataContextPropertyChanged));

Comments and Discussions

Nice work. I am trying to do something similar where I have only a DataGrid in a custom control, and I want to use it in a master-detail LS screen. However, the selected row in the datagrid doesn't update the query's underlying SelectedItem property, which is what the detail portion of the screen is bound to. As a result, selecting different rows in the grid does not cause the details to get updated.

I probably need to set up some binding in the custom control but don't know how. Would love to hear if you have any suggestions.