How to create a ListBox in WPF

The ListBox tag represents a ListBox control in XAML.

 

<ListBox></ListBox>

 

The Width and Height properties represent the width and the height of a ListBox.  The Name property represents the name of the control, which is a unique identifier of a control. The Margin property tells the location of a ListBox on the parent control. The HorizontalAlignment and VerticalAlignment properties are used to set horizontal and vertical alignments.

 

The following code snippet sets the name, height, and width of a ListBox control.  The code also sets horizontal alignment to the left and vertical alignment to the top.

 

<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"

                 VerticalAlignment="Top" Width="194" Height="200" />

 

A ListBox control hosts a collection of ListBoxItems. The following code snippet adds items to a ListBox control.

<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"

         VerticalAlignment="Top" Width="194" Height="200">

    <ListBoxItem Content="Coffie"></ListBoxItem>

    <ListBoxItem Content="Tea"></ListBoxItem>

    <ListBoxItem Content="Orange Juice"></ListBoxItem>

    <ListBoxItem Content="Milk"></ListBoxItem>

    <ListBoxItem Content="Iced Tea"></ListBoxItem>

    <ListBoxItem Content="Mango Shake"></ListBoxItem>

</ListBox>