Mega Code Archive

 
Categories / VB.Net Tutorial / WPF
 

Create a style that will produce a horizontal ListBox

<Window Background="cornsilk"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     x:Class="ListBox_Index.Window1">   <Grid>     <Grid.ColumnDefinitions>       <ColumnDefinition/>     </Grid.ColumnDefinitions>     <Grid.RowDefinitions>       <RowDefinition/>     </Grid.RowDefinitions>     <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" 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>     <Label Margin="10, 10, 3, 3" Name="label1" Grid.Column="0" Grid.Row="5"/>   </Grid> </Window> //File:Window.xaml.vb Imports System Imports System.ComponentModel Imports System.Windows Imports System.Windows.Controls Imports System.Windows.Documents Imports System.Windows.Navigation Imports System.Windows.Shapes Imports System.Windows.Data Namespace ListBox_Index   Public Partial Class Window1     Inherits Window     Public Sub New()       InitializeComponent()     End Sub     Private Sub PrintText(sender As Object, args As SelectionChangedEventArgs)       Dim lbi As ListBoxItem = TryCast(TryCast(sender, ListBox).SelectedItem, ListBoxItem)       If lbi Is Nothing Then         Return       End If       label1.Content = "You chose " & lbi.Content.ToString() & "."     End Sub   End Class End Namespace