Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This example shows how to create a horizontal ListBox by defining a style. The ListBoxItem controls are listed horizontally and separated by user-defined separators. The style sets the ItemsPanel property of the ListBox to a horizontal StackPanel. The following example shows the styles for the Separator and the ListBox.
Example
<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>
For the complete sample, see Horizontal ListBox Sample.
You can also create a horizontal ListBox by creating a new ControlTemplate. For more information, see the example in ItemsPanel.