ListBox
The ListBox
control enables listing a series of items. The good news is that you are not limited to text items but you can also add complex items. Each item is represented by a ListBoxItem
object, nested in the ListBox
. The following example shows how you can declare a ListBox
in XAML code:
Typically a ListBox
is populated at runtime via data-binding. Basically concepts are the same as illustrated for the ComboBox
control, so take a look there for a recap. To accomplish data-binding, simply specify the ItemsSource
markup extension as follows:
<ListBox Name="ListBox1" ItemsSource="{Binding}"/>
Then in Visual Basic code you assign the ItemsSource
property with a data-source as demonstrated in the following LINQ query that returns a list of names about running processes:
You access items in the ListBox
via some properties such as the following:
• SelectedItem
, of type Object
, which returns the instance of the selected item in the ListBox
. The returned object must be converted into the appropriate type.
• Items
, which returns a read-only collection of items in the control.
In Chapter 35 you see a more extensive example of data-binding using the ListBox
. Figure 32.10 shows the result for the data-bound implementation.
The ListBox
control is generally intended for presenting data, even if you can customize items’ template with TextBox
controls. For two-way data-binding, prefer the DataGrid
control described in Chapter 35.
3.131.152.166