Freigeben über


Gewusst wie: Erstellen eines horizontalen ListBox-Elements

Aktualisiert: November 2007

In diesem Beispiel wird gezeigt, wie ein horizontales ListBox-Element durch Definieren eines Stils erstellt wird. Die ListBoxItem-Steuerelemente werden horizontal aufgelistet und durch benutzerdefinierte Trennzeichen getrennt. Der Stil legt die ItemsPanel-Eigenschaft des ListBox-Elements auf einen horizontalen StackPanel fest. Im folgenden Beispiel werden die Stile für Separator und ListBox angezeigt.

Beispiel

<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>

Das vollständige Beispiel finden Sie unter Beispiel für eine horizontale ListBox.

Sie können auch ein horizontales ListBox-Element erstellen, indem Sie eine neue ControlTemplate erstellen. Weitere Informationen finden Sie im Beispiel in ItemsPanel.