Jhon Alexis Mendez
Jhon Alexis Mendez

Reputation: 3

how to get the content of selected item in combo box?

i am trying to get the value of combo box selected item, but instead it returns this "BakeShop.Category".

namespace BakeShop
{

class Oclist
{

    public ObservableCollection<Category> Categories { get; set; }
    public Oclist()
    {
        Categories = new ObservableCollection<Category>
        {
            new Category() { Name = "Dry Ingridients", Series = new ObservableCollection<string>()
            { "Flour", "Cake Flour", "Baking Soda" } },

            new Category() { Name = "Wet Ingridients", Series = new ObservableCollection<string>()
            { "Egg", "Coffee liqueur", "Vodka" } },

            new Category() { Name = "Chocolate", Series = new ObservableCollection<string>()
            { "Dark", "Light", "Crushed", "Chips"} }
        };
    }
}
public class Category
{
    public string Name { get; set; }
    public ObservableCollection<string> Series { get; set; }

xaml:

<ComboBox x:Name="CategoryCBox"
                      ItemsSource="{Binding Categories}"
                      DisplayMemberPath="Name"
                      MaxDropDownHeight="100"
                      Height="20" SelectedIndex="0"
                      FontSize="11"/>

<ComboBox x:Name="TypeCBox"
                      ItemsSource="{Binding SelectedItem.Series, ElementName=CategoryCBox}"
                      SelectionChanged="TypeCBox_SelectionChanged"
                      SelectedIndex="0"
                      Height="20"
                      FontSize="11"/>

and when i do this

string Selected = CategoryCBox.SelectionBoxItem.ToString()
MessageBox.Show(Selected);

it shows "BakeShop.Category"

thanks guys! :)

Upvotes: 0

Views: 537

Answers (3)

Emile van Rooyen
Emile van Rooyen

Reputation: 39

In the ComboBox, create a binding to the selected item

<ComboBox x:Name="CategoryCBox"
                      ItemsSource="{Binding Categories}"
                      DisplayMemberPath="Name"
                      MaxDropDownHeight="100"
                      SelectedItem ="{Binding CategoryCBoxItem}"
                      Height="20" SelectedIndex="0"
                      FontSize="11"/>

Then in your View Model, Create a binding property

private string categoryCBoxItem;
public string CategoryCBoxItem
{
    get { return categoryCBoxItem; }
    set { SetProperty(ref categoryCBoxItem, value); }
}

Upvotes: 0

Suresh
Suresh

Reputation: 4149

You are using DataBinding to set ItemsSource on both ComboBoxes, but then using SelectionChanged event to get data from those out. I think, the right way would be to use DataBinding to get selected values as well.

Your XAML would look like something below:

<ComboBox x:Name="CategoryCBox"
          ItemsSource="{Binding Categories}"
          DisplayMemberPath="Name"
          MaxDropDownHeight="100"
          Height="20" 
          SelectedItem="{Binding SelectedCategory}"
          FontSize="11"/>

<ComboBox x:Name="TypeCBox"
          ItemsSource="{Binding SelectedItem.Series, ElementName=CategoryCBox}"
          SelectedItem="{Binding SelectedSeries}"
          Height="20"
          FontSize="11"/>

You need to also add these two properties: SelectedCategory and SelectedSeries to the Oclist class.

public Category SelectedCategory { get; set; }

public string SelectedSeries { get; set; }

Read more about DataBinding on docs here

Also, here is an excellent blogpost on WPF ComboBox.

EDIT

If you just want to get your code working, then change this to the following;

string Selected = ((Category)CategoryCBox.SelectedItem).Name;
MessageBox.Show(Selected);

But the right way would be to go with proper DataBinding approach.

Upvotes: 0

Nam Vi Nguyen
Nam Vi Nguyen

Reputation: 83

You can get Category from SelectedItem property of ComboBox.

SelectedItem of ComboBox is an object thus you need to parse it into Category.

Example:

  1. Category category = yourComboBox.SelectedItem as Category or
  2. Category category = (Category)yourComboBox.SelectedItem

Upvotes: 1

Related Questions