Reputation: 3
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
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
Reputation: 4149
You are using DataBinding to set ItemsSource
on both ComboBox
es, 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.
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
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:
Category category = yourComboBox.SelectedItem as Category
orCategory category = (Category)yourComboBox.SelectedItem
Upvotes: 1