Search This Blog

Tuesday, June 3, 2014

ComboBoxItem WPF Example

WPF > Controls  > ContentControl > ComboBoxItem 

ComboBoxItem represent an item in a ComboBox.


Example:

XAML


<Window x:Class="WpfApplication3.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 x:Name="cbo" HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" SelectionChanged="ComboBox_SelectionChanged">
            <ComboBoxItem>Option1</ComboBoxItem>
            <ComboBoxItem>Option2</ComboBoxItem>
        </ComboBox>

    </Grid>
</Window>

C#
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  string value = ((ComboBoxItem)cbo.SelectedItem).Content.ToString();
}