Binding SelectedValue of ComboBox to enum in WPF?

If you are ready to change the binding of the ItemsSource of the ComboBox then, simply SelectedValue="{Binding Mode,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" will work.

Up vote 0 down vote favorite 1 share g+ share fb share tw.

I want to show list of products in ListView where one of the columns is a ComboBox that I want to bind. This is my enum: public enum SelectionMode { One, Two } And Product class: public class Product { public SelectionMode Mode { get; set; } public string Name { get; set; } } In ViewModel class I have an ObservableCollection of Product's: private ObservableCollection _productList; public ObservableCollection ProductList { get { return _productList; } set { _productList = value; } } public MainViewModel() { ProductList = new ObservableCollection { new Product {Mode = SelectionMode. One, Name = "One"}, new Product {Mode = SelectionMode.

One, Name = "One"}, new Product {Mode = SelectionMode. Two, Name = "Two"} }; } And finally I have a Grid with a ListView that binds to my ProductList: My question is; what is the way to bind SelectedValue of ComboBox to SelectionMode property of my Product class? Update Well.

I found an answer in this topic. So I have to add converter class: public class MyEnumToStringConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization. CultureInfo culture) { return value.ToString(); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.

CultureInfo culture) { return (SelectionMode)Enum. Parse(typeof(SelectionMode), value.ToString(), true); } } And add it to window resources: And finally edit ComboBox data template: That's all. Hope it will be useful for someone else :) c# wpf xaml data-binding wpf-controls link|improve this question edited Jan 14 at 18:06Samuel Slade2,9452520 asked Dec 27 '11 at 11:56shtkuh995 71% accept rate.

You can bind your combobox to int and then Enum. Parse(...) – Elastep Dec 27 '11 at 12:00.

If you are ready to change the binding of the ItemsSource of the ComboBox then, simply SelectedValue="{Binding Mode,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" will work. In this case you have to bind the ItemsSource like this: ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ViewClass}}, Path=ModeList}"; where, the ModeList is a simple public property of list of SelectionMode type, contains the enums which should be displayed in ComboBox dropdown and ViewClass is the class where this property (ModeList) is a available; make sure the reference of the namespace is added in the xaml. Otherwise you have to use a converter, which should convertback the string to the enum type.

I'm using a Converter for this, which also allows to define a string that will be displayed instead of the enum value: ageektrapped.com/blog/the-missing-net-7-....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions