WPF: XAML ComboBox

This entry is part 26 of 54 in the series Learn WPF

A ComboBox is just like a drop down list in HTML.

Here we use some XAML tags to work out a ComboBox.  We have made the ComboBox to be editable, so you can actually type in the box to make a selection.

<Window x:Class="ComboBox2.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Margin="20,20,20,20">
        <TextBox Width="200" Height="30" HorizontalAlignment="left">Where would you like to go today?</TextBox>
        <ComboBox Name="cb" SelectionChanged="DisplayOutput" Text="My Cities" IsEditable="true">
        <TextBlock Name="tb" Width="200" Height="30" HorizontalAlignment="Center" Margin="10,100,50,50">You selected</TextBlock>

using System.Windows;
using System.Windows.Controls;

namespace ComboBox2
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
        public MainWindow()

        void DisplayOutput(object sender, SelectionChangedEventArgs args)
            ComboBoxItem cb = ((sender as ComboBox).SelectedItem as ComboBoxItem);
            tb.Text = "You selected " + cb.Content.ToString() + ".";



Series Navigation<< WPF: Simple combobox data binding
WPF: Combobox binding >>

Leave a comment

Leave a Reply