Share via


Cómo: Cambiar el diseño de un control DataRepeater (Visual Studio)

El control DataRepeater se puede mostrar en una orientación vertical (los elementos se desplazan verticalmente) u horizontal (los elementos se desplazan horizontalmente). Puede cambiar en tiempo de diseño o en tiempo de ejecución la orientación modificando la propiedad LayoutStyle. Si modifica la propiedad LayoutStyle en tiempo de ejecución, es posible que desee cambiar también el tamaño de ItemTemplate y la posición de los controles secundarios.

NotaNota

Si cambia la posición de los controles en ItemTemplate en tiempo de ejecución, deberá llamar a los métodos EndResetItemTemplate y BeginResetItemTemplate al principio y al final del bloque de código que cambia la posición de los controles.

Para cambiar el diseño en tiempo de diseño

  1. En el Diseñador de Windows Forms, seleccione el control DataRepeater.

    NotaNota

    Debe seleccionar el borde exterior del control DataRepeater haciendo clic en la región inferior del control y no en la región ItemTemplate superior.

  2. En la ventana Propiedades, establezca la propiedad LayoutStyle en Vertical o Horizontal.

Para cambiar el diseño en tiempo de ejecución

  1. Agregue el código siguiente al controlador de eventos Click de un botón o menú:

    ' 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. En la mayoría de los casos, es posible que desee agregar un código similar al mostrado en la sección de ejemplos para cambiar el tamaño de ItemTemplate y reorganizar los controles a fin de ajustarlos a la nueva orientación.

Ejemplo

En el ejemplo siguiente se muestra cómo responder al evento LayoutStyleChanged en un controlador de eventos. Este ejemplo requiere tener un control DataRepeater denominado DataRepeater1 en un formulario y que su ItemTemplate contenga dos controles TextBox denominados TextBox1 y TextBox2.

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

Vea también

Tareas

Solución de problemas del control DataRepeater (Visual Studio)

Cómo: Cambiar la apariencia de un control DataRepeater (Visual Studio)

Referencia

DataRepeater

LayoutStyle

BeginResetItemTemplate

EndResetItemTemplate

Conceptos

Introducción al control DataRepeater (Visual Studio)