Listing items in a Listbox control
In WPF, the ListBox
control is used to display a list of items. Users can select one or more items from the list, depending on the SelectionMode
specified.
In this recipe, we are going to learn how to create a ListBox
control and use it in WPF applications.
Getting ready
Open your Visual Studio IDE and create a new WPF application project, called CH02.ListBoxDemo
.
How to do it...
Adding a ListBox
control in the UI is as easy as writing a <ListBox />
tag in any XAML page. But to hold the data in it, you will have to use its properties properly. Follow these steps to add a ListBox
control with some static data:
- Open the
MainWindow.xaml
page of the WPF project. - Under the default
Grid
panel, add the<ListBox></ListBox>
tag to add the control. - Add a few
ListBoxItem
inside the control, as shared here:
<ListBox x:Name="lstBox" Width="120" Height="85" Margin="10 10 20 5"> <ListBoxItem Content="Item 1" /> <ListBoxItem...