如何:使用代码修改 Silverlight 屏幕

通过 Visual Studio LightSwitch,您可以通过使用设计器和工具窗口完成许多与屏幕相关的设计任务。但是,有一些您可能要通过使用代码完成的特定任务。本主题说明了如何通过使用屏幕对象模型完成一组常见屏幕相关的设计任务。有关可以在应用程序中编写代码的位置的更多信息,请参阅下列任意主题:

有关在 Visual Studio LightSwitch 中编写代码的一般性指导,请参见 在 LightSwitch 中编写代码

常规任务

下面的列表描述了可通过使用屏幕对象模型完成且与常见数据相关的任务。

  • 使控件隐藏、只读或禁用

使控件隐藏、只读或禁用

您可以通过使用代码隐藏或显示在屏幕上的控件。您还可以指定控件是只读还是禁用。

如果公司名称是 Coho Winery,则下面的示例会将公司名称隐藏在数据网格中。本示例还使控件处于只读模式,这样查看者就不能通过向控件键入文本来修改公司名称。

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++;
    }

}

如果选定项目的公司名称是 Coho Winery,则下面的示会例将公司名称隐藏在屏幕上的详细信息视图中。本示例还会禁用**“删除”**按钮,使用户无法删除适用于 Coho Winery 的客户。

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

请参见

任务

如何:处理 Silverlight 屏幕事件

概念

在 LightSwitch 中编写代码

使用代码执行与数据相关的任务

其他资源

屏幕:应用程序的用户界面