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:

Common Tasks

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

  • Making controls hidden, read-only, or disabled

Making Controls Hidden, Read-Only, or Disabled

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 Sub FindControlInList()
    Dim index As Integer = 0

    For Each cust As Customer In Customers

        If cust.CompanyName = "Great Lakes Food Market" Then 
            With FindControlInCollection("CompanyName", Customers(index))
                .IsVisible = False
                .IsReadOnly = True 
            End With 

        End If
        index = index + 1
    Next 
End Sub
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.

Private Sub Customers_SelectionChanged()
    FindControl("Customers_DeleteSelected").IsEnabled = True 

    If Me.Customers.SelectedItem.CompanyName = "Great Lakes Food Market" Then

        FindControl("CompanyName1").IsVisible = False
        FindControl("Customers_DeleteSelected").IsEnabled = False 

    End If 

End Sub
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;
    }
}

See Also

Tasks

How to: Handle Silverlight Screen Events

Concepts

Performing Data-Related Tasks by Using Code

Other Resources

Screens: The User Interface of Your LightSwitch Application

Writing Code in LightSwitch