C# WPF XAML ListBox


This entry is part 1 of 6 in the series WPF ListBox

The book Illustrated WPF discusses list box controls. A ListBox is a collection of items displayed to the user, from which he or she can select one or more items. As with all ItemsControls, the items in the ListBox can be of any type derived from the UIElement class, including Images and Buttons. By default, a ListBox sets its width to the width of its widest item.

There are two forms in which you can place items into a ListBox’s Items collection—either by explicitly wrapping each item in a ListBoxItem element or by just placing them in the collection and letting the system implicitly wrap them.

Here is what it looks like and the markup and code is also listed.

ListBoxOne

1<Window x:Class="ListBoxOne.MainWindow"
6        xmlns:local="clr-namespace:ListBox"
7        mc:Ignorable="d"
8        Title="List Box One" Height="150" Width="300">
9    <StackPanel>
10        <ListBox Name="lstbxCats" HorizontalAlignment="Left" Width="100">
11            <ListBoxItem>Johnny</ListBoxItem>
12            <ListBoxItem>Billy</ListBoxItem>
13            <ListBoxItem>Sammy</ListBoxItem>
14        </ListBox>
15        <Button Click="Button_Click" HorizontalAlignment="Left" Width="100" Padding="10,3" Margin="0,5">Enter</Button>
16    </StackPanel>
17</Window>

Below is the code-behind.

1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows;
7using System.Windows.Controls;
8using System.Windows.Data;
9using System.Windows.Documents;
10using System.Windows.Input;
11using System.Windows.Media;
12using System.Windows.Media.Imaging;
13using System.Windows.Navigation;
14using System.Windows.Shapes;
15namespace ListBoxOne
16{
17    /// <summary>
18    /// Interaction logic for MainWindow.xaml
19    /// </summary>
20    public partial class MainWindow : Window
21    {
22        public MainWindow()
23        {
24            InitializeComponent();
25        }
26        private void Button_Click(object sender, RoutedEventArgs e)
27        {
28            // display the user's selection in the message box
29            object obj = lstbxCats.SelectedItem;
30            // use the conditional operator which is like the if statement
31            //
32            string selected = (obj == null)
33                ? "No item selected."
34                : (string)((ListBoxItem)obj).Content;
35            MessageBox.Show(selected, "Selected Item");
36        }
37    }
38}

C# Language Conditional Operator

The condition must evaluate to true or false. If condition is true, first_expression is evaluated and becomes the result. If condition is false, second_expression is evaluated and becomes the result. Only one of the two expressions is evaluated. Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other. You can express calculations that might otherwise require an if-else construction more concisely by using the conditional operator. The conditional operator cannot be overloaded.

1condition ? first_expression : second_expression;

You can place items in the ListBox’s Items collection, even without explicitly creating ListBoxItems for them, as shown in the following markup. Rather than creating explicit ListBoxItems, it inserts two TextBlocks and a Button.

Series NavigationWPF ListBox Selection >>