Friday 22 February 2013

LightSwitch: Add Non Existent Records Using AutoCompleteBox

The LightSwitch AutoCompleteBox allows you select records using a dropdown list or to enter some text into the control. Typing into the AutoCompleteBox filters down the selectable records.

If a non existent record entered, it’s likely that a new record is required based upon the entered text. 

The following example uses a set of Customer and Category tables. A Customer belongs to a Category and the following sample demonstrates a ‘New Data Screen’ for creating a new Customer. The screen contains an AutoCompleteBox which allows a category to be selected. If a non existent category is typed in, the user will be prompted to save the new category prior to saving the customer record.


We need to write some code to handle the LostFocus event of the AutoCompleteBox. However, this can only be done when the AutoCompleteBox becomes available. We therefore need to write an event handler for the ‘ControlAvailable’ event and to add the handler for the LostFocus event there. Click on the ‘Write Code’ button and click and select the ‘CreateNewCustomer_Created’ event. Add the following code:

private void CreateNewCustomer_Created()
    this.FindControl("Category").ControlAvailable += CategoryFieldAvailable;

private void CategoryFieldAvailable(object sender, ControlAvailableEventArgs e)
    ((System.Windows.Controls.Control)e.Control).LostFocus += CategoryFieldChanged;

private void CategoryFieldChanged(object sender, System.Windows.RoutedEventArgs e)
    //Add a reference to System.Windows.Controls.Input.dll in the Client project
    string txtComboText = ((System.Windows.Controls.AutoCompleteBox)sender).Text;
    this.Details.Dispatcher.BeginInvoke(() =>
        if (!string.IsNullOrEmpty(txtComboText)) {
            Category selectedCategory = this.DataWorkspace.ApplicationData.Categories.Where(Category => Category.CategoryName == txtComboText).FirstOrDefault;
            if (selectedCategory == null) {
                //Category doesn't exists
                if (this.ShowMessageBox("Do you want to add the category " + txtComboText + "?", "Add Category", MessageBoxOption.YesNo) == System.Windows.MessageBoxResult.Yes) {
                    selectedCategory = this.DataWorkspace.ApplicationData.Categories.AddNew();
                    selectedCategory.CategoryName = txtComboText;
                    this.CustomerProperty.Category = selectedCategory;

In the above code, we obtain the text that has been entered using the 
‘Text’ property of the AutoCompleteBox. If the text is not empty, we 
attempt to look up the Category in the Category table. If the Category 
isn’t found, we use ‘ShowMessageBox’ to prompt the user to save the new 
Category. After the new Category is saved, we set the Category property 
on the Customer entity equal to the Category which has just been 
Now run the application. Here are some illustrations showing the 
application in action. We begin by having just a ‘High Risk’ Category in
 the database. 
In the ‘Create New Customer’ form, we enter a Firstname, Surname and type ‘Low Risk’ into the Category AutoCompleteBox. When we leave the Category AutoCompleteBox, the following dialog appears.
If we click ‘Yes’, the Customer record is saved into the database. If we
 now return to the Category screen, we see that the ‘Low Risk’ category 
has also been added into the database. 
Your new entry has been saved.
LightSwitch: Textbox LostFocus Event  

No comments: