次の方法で共有


DataRepeater.ItemTemplate Property

Gets a template that represents the DataRepeaterItem for a DataRepeater control.

Namespace:  Microsoft.VisualBasic.PowerPacks
Assembly:  Microsoft.VisualBasic.PowerPacks.Vs (in Microsoft.VisualBasic.PowerPacks.Vs.dll)

Syntax

'宣言
<BrowsableAttribute(False)> _
Public ReadOnly Property ItemTemplate As DataRepeaterItem
    Get
[BrowsableAttribute(false)]
public DataRepeaterItem ItemTemplate { get; }
[BrowsableAttribute(false)]
public:
property DataRepeaterItem^ ItemTemplate {
    DataRepeaterItem^ get ();
}
[<BrowsableAttribute(false)>]
member ItemTemplate : DataRepeaterItem
function get ItemTemplate () : DataRepeaterItem

Property Value

Type: Microsoft.VisualBasic.PowerPacks.DataRepeaterItem
An object that determines the layout and appearance of items in the DataRepeater control.

Remarks

Use the ItemTemplate property to set properties that determine the run time appearance of all items in a DataRepeater control. In effect, you are actually setting properties of the DataRepeaterItem objects that will be created at run time. The items that you see in the DataRepeater at run time are copied from this object as they are scrolled into view.

At design time, when you select the section of the DataRepeater that contains the controls, the Properties window contains properties for the ItemTemplate. For example, setting the BackColor property of the ItemTemplate to Red causes all the items in the DataRepeater to be displayed with a red background.

At run time, if you change the properties of the ItemTemplate, you must call the BeginResetItemTemplate method before setting the properties, and then call the EndResetItemTemplate method to commit the changes.

The ItemTemplate property is also used to access the controls in the DataRepeaterItem at run time. For example, to retrieve the BackColor of a TextBox named IDTextBox, you would use the following code:

Dim BC As Color = _
 DataRepeater1.ItemTemplate.Controls(IDTextBox.Name).BackColor
color BC = dataRepeater1.ItemTemplate.Controls["IDTextBox"].BackColor;

Examples

Some common customizations for the DataRepeater control include displaying the rows in alternating colors and changing the color of a field based on a condition. The following example shows how to perform these customizations. This example assumes that you have a DataRepeater control that is bound to the Products table in the Northwind database.

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.
        e.DataRepeaterItem.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.
        e.DataRepeaterItem.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;
    }
}

.NET Framework Security

See Also

Reference

DataRepeater Class

Microsoft.VisualBasic.PowerPacks Namespace

BeginResetItemTemplate

EndResetItemTemplate

Other Resources

Introduction to the DataRepeater Control (Visual Studio)

How to: Change the Appearance of a DataRepeater Control (Visual Studio)