WPF ComboBox Example (ItemsSource)Use the WPF ComboBox control with the ItemsSource property in C# code.
ComboBox. This WPF control is useful: it is a drop-down list of strings. The user clicks on the box, and the items appear. We set items with ItemsSource. With IsEditable, the ComboBox accepts input text.
First, we create a new WPF project and drag a ComboBox to the Window. This causes Visual Studio to insert the ComboBox element in the XAML file. We then specify the Loaded and SelectionChanged events.
Tip For event handlers in WPF, please type in the attribute, such as "Loaded" and Visual Studio will create the C# event handler.
Loaded. We use the ComboBox_Loaded event handler to specify the items in the control at runtime. We do not need to know the items at design-time. We assign a List of strings to the ItemsSource.
Also We set the SelectionIndex property to 0. This sets the first element in the List in the ComboBox.
Example markup, XAML:
<Window x:Class="WpfApplication9.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <ComboBox HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="120" Loaded="ComboBox_Loaded" SelectionChanged="ComboBox_SelectionChanged"/> </Grid> </Window>
using System.Collections.Generic; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace WpfApplication9 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void ComboBox_Loaded(object sender, RoutedEventArgs e) { // ... A List. List<string> data = new List<string>(); data.Add("Book"); data.Add("Computer"); data.Add("Chair"); data.Add("Mug"); // ... Get the ComboBox reference. var comboBox = sender as ComboBox; // ... Assign the ItemsSource to the List. comboBox.ItemsSource = data; // ... Make the first item selected. comboBox.SelectedIndex = 0; } private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { // ... Get the ComboBox. var comboBox = sender as ComboBox; // ... Set SelectedItem as Window Title. string value = comboBox.SelectedItem as string; this.Title = "Selected: " + value; } } }
SelectionChanged. We also use the ComboBox_SelectionChanged event handler. We display the currently selected item in the ComboBox as part of the Window Title. The string could be used in any way in the C# code.
ItemsSource. The ItemsSource is a property on the ComboBox. It can be assigned to any reference object that implements the IEnumerable interface. This includes arrays and Lists. We can set all the items in a single assignment.
Also We can read in the items from a text file containing strings, and then use those values (stored in a List or array) as the items.
Summary. A ComboBox presents the user with a choice. We populate the ComboBox in various ways. The ItemsSource property is one of the easiest—it receives an IEnumerable collection. And in event handlers (SelectionChanged) we act on user input.
© 2007-2022 sam allen.
see site info on the changelog.