WPF: Simple combobox data binding

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

Here we will just work out a simple way to display a selected item in a combo box using data binding method.

This is all done with XAML without using code behind.

MainWindow.xaml
<Window x:Class="ComboBox2.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">
    <StackPanel Margin="20,20,20,20">
        <ComboBox Name="cbox" Text="My Cities" IsEditable="True" IsReadOnly="True">
            <ComboBoxItem>Spain</ComboBoxItem>
            <ComboBoxItem>France</ComboBoxItem>
            <ComboBoxItem>Peru</ComboBoxItem>
            <ComboBoxItem>Mexico</ComboBoxItem>
            <ComboBoxItem>Italy</ComboBoxItem>
        </ComboBox>
        <TextBlock Text="{Binding ElementName=cbox, Path=SelectedItem.Content}" Width="200" Height="30" HorizontalAlignment="Center" Margin="10,100,50,50"></TextBlock>
    </StackPanel>
</Window>

simplecombo

 

 

 

 

Series Navigation<< WPF: Binding to a single object
WPF: XAML ComboBox >>

Leave a comment

Leave a Reply