如何:更改 DataRepeater 控件的布局 (Visual Studio)

更新:2007 年 11 月

可以沿垂直(项目垂直滚动)方向或水平(项目水平滚动)方向显示 DataRepeater 控件。您可以通过在设计时或运行时更改 LayoutStyle 属性来更改方向。如果在运行时更改 LayoutStyle 属性,您可能还需要重新调整 ItemTemplate 的大小和重新定位子控件。

说明:

如果在运行时在 ItemTemplate 上重新定位控件,您需要在用于重新定位控件的代码块的开头和结尾处调用 BeginResetItemTemplateEndResetItemTemplate 方法。

在设计时更改布局

  1. 在 Windows 窗体设计器中,选择 DataRepeater 控件。

    说明:

    必须选择 DataRepeater 控件的外部边框,方法是单击该控件的下半部分区域,而不是单击上半部分的 ItemTemplate 区域。

  2. 在“属性”窗口中,将 LayoutStyle 属性设置为 VerticalHorizontal

在运行时更改布局

  1. 在按钮或菜单的 Click 事件处理程序中添加下面的代码:

    ' Switch the orientation.
    If DataRepeater1.LayoutStyle = _
     PowerPacks.DataRepeaterLayoutStyles.Vertical Then
        DataRepeater1.LayoutStyle = _
         PowerPacks.DataRepeaterLayoutStyles.Horizontal
    Else
        DataRepeater1.LayoutStyle = _
         PowerPacks.DataRepeaterLayoutStyles.Vertical
    End If
    
    // Switch the orientation.
    if (dataRepeater1.LayoutStyle == DataRepeaterLayoutStyles.Vertical)
    {
        dataRepeater1.LayoutStyle = DataRepeaterLayoutStyles.Horizontal;
    }
    else
    {
        dataRepeater1.LayoutStyle = DataRepeaterLayoutStyles.Vertical;
    }            
    
  2. 在大多数情况下,您会希望添加类似于“示例”部分所示的代码来重新调整 ItemTemplate 的大小并重新排列控件,以适应新的方向。

示例

下面的示例演示如何在事件处理程序中响应 LayoutStyleChanged 事件。此示例要求您在窗体上有一个名为 DataRepeater1 的 DataRepeater 控件,并且该控件的 ItemTemplate 包含两个名称分别为 TextBox1 和 TextBox2 的 TextBox 控件。

Private Sub DataRepeater1_LayoutStyleChanged(ByVal sender As Object, _
 ByVal e As System.EventArgs) Handles DataRepeater1.LayoutStyleChanged
    ' Call a method to re-initialize the template.
    DataRepeater1.BeginResetItemTemplate()
    If DataRepeater1.LayoutStyle = _
     PowerPacks.DataRepeaterLayoutStyles.Vertical Then
        ' Change the height of the template and rearrange the controls.
        DataRepeater1.ItemTemplate.Height = 150
        DataRepeater1.ItemTemplate.Controls(TextBox1.Name).Location = _
         New Point(20, 40)
        DataRepeater1.ItemTemplate.Controls(TextBox2.Name).Location = _
         New Point(150, 40)
    Else
        ' Change the width of the template and rearrange the controls.
        DataRepeater1.ItemTemplate.Width = 150
        DataRepeater1.ItemTemplate.Controls(TextBox1.Name).Location = _
         New Point(40, 20)
        DataRepeater1.ItemTemplate.Controls(TextBox2.Name).Location = _
         New Point(40, 150)
    End If
    ' Apply the changes to the template.
    DataRepeater1.EndResetItemTemplate()
End Sub
private void dataRepeater1_LayoutStyleChanged_1(object sender, EventArgs e)
{
    // Call a method to re-initialize the template.
    dataRepeater1.BeginResetItemTemplate();
    if (dataRepeater1.LayoutStyle == DataRepeaterLayoutStyles.Vertical)
    // Change the height of the template and rearrange the controls.
    {
        dataRepeater1.ItemTemplate.Height = 150;
        dataRepeater1.ItemTemplate.Controls["TextBox1"].Location = new Point(20, 40);
        dataRepeater1.ItemTemplate.Controls["TextBox2"].Location = new Point(150, 40);
    }
    else
    {
        // Change the width of the template and rearrange the controls.
        dataRepeater1.ItemTemplate.Width = 150;
        dataRepeater1.ItemTemplate.Controls["TextBox1"].Location = new Point(40, 20);
        dataRepeater1.ItemTemplate.Controls["TextBox2"].Location = new Point(40, 150);
    }
    // Apply the changes to the template.
    dataRepeater1.EndResetItemTemplate();
}

请参见

任务

DataRepeater 控件疑难解答 (Visual Studio)

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

概念

DataRepeater 控件简介 (Visual Studio)

参考

DataRepeater

LayoutStyle

BeginResetItemTemplate

EndResetItemTemplate

修订记录

日期

修订

原因

2008 年 7 月

新增主题。

SP1 功能更改。