How to: Modify a Silverlight Screen by Using Code

Visual Studio LightSwitch enables you to accomplish many screen-related design tasks by using designers and tool windows. However, there are certain tasks that you might want to accomplish by using code. This topic shows you how to accomplish a set of common screen-related design tasks by using the screen object model. For more information about where you can write code in your application, see any of the following topics:

The following list describes some common data-related tasks that you accomplish by using the screen object model.

You can hide or show controls on a screen by using code. You can also specify whether controls are read-only or disabled.

The following example hides a company name in a data grid if the company name is Great Lakes Food Market. This example also makes the control read-only so that viewers cannot modify the company name by typing text into the control.

private void FindControlInList()
{
    int index = 0;

    foreach (Customer cust in this.Customers)
    {
        if (cust.CompanyName == "Great Lakes Food Market")
        {
            this.FindControlInCollection("CompanyName",
            this.Customers.ElementAt(index)).IsVisible = false;
            this.FindControlInCollection("CompanyName",
            this.Customers.ElementAt(index)).IsReadOnly = true;
        }

        index++;
    }

}

The following example hides the company name in a details view on the screen if the company name of the selected item is Great Lakes Food Market. This example also disables the Delete button so that users cannot delete a customer who works for Great Lakes Food Market.

partial void Customers_SelectionChanged()
{
    this.FindControl("Customers_DeleteSelected").IsEnabled = true;

    if (this.Customers.SelectedItem.CompanyName == "Great Lakes Food Market")
    {
        this.FindControl("CompanyName1").IsVisible = false;
        this.FindControl("Customers_DeleteSelected").IsEnabled = false;
    }
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft