如何:更改 DataRepeater 控件的外观 (Visual Studio)

更新:2007 年 11 月

可通过在设计时设置属性或在运行时处理 DrawItem 事件来更改 DataRepeater 控件的外观。

在运行时将对每个 DataRepeaterItem 重复您在设计时在选中控件的项模板部分的情况下设置的属性。仅当容器没有被完全覆盖时(例如,Padding 属性设置为较大的值时),才能在运行时看到 DataRepeater 控件本身与外观相关的属性。

在运行时,可基于条件设置与外观相关的属性。例如,在日程安排应用程序中,您可以更改某个项的背景颜色来警告用户该项已过期。在 DrawItem 事件处理程序中,如果在诸如 If¡­Then 这样的条件语句中设置属性,还必须使用 Else 子句来指定不满足条件时的外观。

在设计时更改外观

  1. 在 Windows 窗体设计器中选择 DataRepeater 控件的项模板(上半部分)区域。

  2. 在“属性”窗口中选择某个属性并更改它的值。影响外观的常见属性包括 BackColorBackgroundImageBorderStyleForeColor

在运行时更改外观

  1. 在代码编辑器中,在“事件”下拉列表中单击“DrawItem”。

  2. DrawItem 事件处理程序中添加下面的代码来设置属性:

    ' Set the default BackColor.
    e.DataRepeaterItem.BackColor = Color.White
    ' Loop through the controls on the DataRepeaterItem.
    For Each c As Control In e.DataRepeaterItem.Controls
        ' Check the type of each control.
        If TypeOf c Is TextBox Then
            ' If a TextBox, change the BackColor.
            c.BackColor = Color.AliceBlue
        Else
            ' Otherwise use the default BackColor.
            c.BackColor = e.DataRepeaterItem.BackColor
        End If
    Next
    
    // Set the default BackColor.
    e.DataRepeaterItem.BackColor = Color.White;
    // Loop through the controls on the DataRepeaterItem.
    foreach (Control c in e.DataRepeaterItem.Controls)
    {
        // Check the type of each control.
        if (c is TextBox)
        // If a TextBox, change the BackColor.
        {
            c.BackColor = Color.AliceBlue;
        }
        else
        {
            // Otherwise use the default BackColor.
            c.BackColor = e.DataRepeaterItem.BackColor;
        }
    }
    

示例

DataRepeater 控件的一些常见自定义包括以交替颜色显示行以及基于条件更改字段的颜色。下面的示例演示如何执行这些自定义项。此示例假定您有一个 DataRepeater 控件,该控件绑定到 Northwind 数据库的 Products 表。

Private Sub DataRepeater1_DrawItem(ByVal sender As Object, ByVal e _
 As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) _
 Handles DataRepeater1.DrawItem
    ' Alternate the back color.
    If (e.DataRepeaterItem.ItemIndex Mod 2) <> 0 Then
        ' Apply the secondary back color.
        e.DataRepeaterItem.BackColor = Color.AliceBlue
    Else
        ' Apply the default back color.
        DataRepeater1.ItemTemplate.BackColor = Color.White
    End If
    ' Change the color of out-of-stock items to red.
    If e.DataRepeaterItem.Controls(UnitsInStockTextBox.Name).Text _
     < 1 Then
        e.DataRepeaterItem.Controls(UnitsInStockTextBox.Name). _
         BackColor = Color.Red
    Else
        e.DataRepeaterItem.Controls(UnitsInStockTextBox.Name). _
         BackColor = Color.White
    End If
End Sub
private void dataRepeater1_DrawItem(object sender, 
    Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs e)
{
    // Alternate the back color.
    if ((e.DataRepeaterItem.ItemIndex % 2) != 0)
    // Apply the secondary back color.
    {
        e.DataRepeaterItem.BackColor = Color.AliceBlue;
    }
    else
    {
        // Apply the default back color.
        dataRepeater1.ItemTemplate.BackColor = Color.White;
    }
    // Change the color of out-of-stock items to red.
    if (e.DataRepeaterItem.Controls["unitsInStockTextBox"].Text == "0")
    {
        e.DataRepeaterItem.Controls["unitsInStockTextBox"].BackColor = Color.Red;
    }
    else
    {
        e.DataRepeaterItem.Controls["unitsInStockTextBox"].BackColor = Color.White;
    }
}

请注意,对于这两个自定义,您必须提供代码来设置满足和不满足条件的属性。如果未指定 Else 条件,您将在运行时看到意外的结果。

请参见

任务

DataRepeater 控件疑难解答 (Visual Studio)

如何:在 DataRepeater 控件中显示绑定数据 (Visual Studio)

如何:在 DataRepeater 控件中显示未绑定的数据 (Visual Studio)

如何:在 DataRepeater 控件中显示项标题 (Visual Studio)

概念

DataRepeater 控件简介 (Visual Studio)

参考

DataRepeater

DrawItem

修订记录

日期

修订

原因

2008 年 7 月

新增主题。

SP1 功能更改。