Compartir a través de


Cómo: Crear un control ListBox horizontal

Actualización: noviembre 2007

En este ejemplo se muestra cómo crear un control ListBox horizontal definiendo un estilo. Los controles ListBoxItem se enumeran horizontalmente y se separan mediante separadores definidos por el usuario. El estilo establece la propiedad ItemsPanel de ListBox en un objeto StackPanel horizontal. En el ejemplo siguiente se muestran los estilos de Separator y ListBox.

Ejemplo

<Grid.Resources>
  <Style TargetType="Separator">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type Separator}">
          <Border Width="2" Height="12" Margin="4" Background="Gray"/>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>

  <Style TargetType="ListBox">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"
                      VerticalAlignment="Center"
                      HorizontalAlignment="Center"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
  </Style>

</Grid.Resources>


...


<ListBox Name="lb" 
         Margin="10, 10, 3, 3" Height="50"
         Grid.Column="0" Grid.Row="2"
         Grid.RowSpan="2"
         SelectionChanged="PrintText">
  <ListBoxItem>Item 1</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 2</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 3</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 4</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 5</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 6</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 7</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 8</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 9</ListBoxItem>
  <Separator/>
  <ListBoxItem>Item 10</ListBoxItem>
</ListBox>

Para obtener el ejemplo completo, vea Ejemplo Horizontal ListBox.

También puede crear un ListBox horizontal creando un nuevo objeto ControlTemplate. Para obtener más información, consulte el ejemplo de ItemsPanel.