Note that there are some explanatory texts on larger screens.

plurals
  1. POBinding SelectedValue of ComboBox to enum in WPF
    primarykey
    data
    text
    <p>I want to show list of products in <code>ListView</code> where one of the columns is a <code>ComboBox</code> that I want to bind. This is my enum:</p> <pre><code>public enum SelectionMode { One, Two } </code></pre> <p>And Product class:</p> <pre><code>public class Product { public SelectionMode Mode { get; set; } public string Name { get; set; } } </code></pre> <p>In <code>ViewModel</code> class I have an <code>ObservableCollection</code> of <code>Product</code>'s:</p> <pre><code> private ObservableCollection&lt;Product&gt; _productList; public ObservableCollection&lt;Product&gt; ProductList { get { return _productList; } set { _productList = value; } } public MainViewModel() { ProductList = new ObservableCollection&lt;Product&gt; { new Product {Mode = SelectionMode.One, Name = "One"}, new Product {Mode = SelectionMode.One, Name = "One"}, new Product {Mode = SelectionMode.Two, Name = "Two"} }; } </code></pre> <p>And finally I have a <code>Grid</code> with a <code>ListView</code> that binds to my <code>ProductList</code>:</p> <pre><code>&lt;Window.Resources&gt; &lt;ObjectDataProvider x:Key="AlignmentValues" MethodName="GetNames" ObjectType="{x:Type System:Enum}"&gt; &lt;ObjectDataProvider.MethodParameters&gt; &lt;x:Type TypeName="ViewModel:SelectionMode" /&gt; &lt;/ObjectDataProvider.MethodParameters&gt; &lt;/ObjectDataProvider&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ListView Height="120" HorizontalAlignment="Left" VerticalAlignment="Top" SelectionMode="Multiple" ItemsSource="{Binding ProductList}" &gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn Width="120" Header="Product Name" DisplayMemberBinding="{Binding Path=Name}" /&gt; &lt;GridViewColumn Header="Selection Mode"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding Source={StaticResource AlignmentValues}}"/&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;/Grid&gt; </code></pre> <p>My question is; what is the way to bind <code>SelectedValue</code> of <code>ComboBox</code> to <code>SelectionMode</code> property of my <code>Product</code> class?</p> <p><strong>Update</strong></p> <p>Well. I found an answer in <a href="http://geekswithblogs.net/cskardon/archive/2008/10/16/databinding-an-enum-in-wpf.aspx" rel="nofollow">this topic</a>. So I have to add converter class:</p> <pre><code>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); } } </code></pre> <p>And add it to window resources:</p> <pre><code>&lt;Window.Resources&gt; &lt;ObjectDataProvider x:Key="AlignmentValues" MethodName="GetNames" ObjectType="{x:Type System:Enum}"&gt; &lt;ObjectDataProvider.MethodParameters&gt; &lt;x:Type TypeName="ViewModel:SelectionMode" /&gt; &lt;/ObjectDataProvider.MethodParameters&gt; &lt;/ObjectDataProvider&gt; &lt;Converters:MyEnumToStringConverter x:Key="MyEnumConverter"/&gt; &lt;/Window.Resources&gt; </code></pre> <p>And finally edit <code>ComboBox</code> data template:</p> <pre><code>&lt;ComboBox ItemsSource="{Binding Source={StaticResource AlignmentValues}}" SelectedValue="{Binding Path=Mode, Converter={StaticResource MyEnumConverter}}"/&gt; </code></pre> <p>That's all. Hope it will be useful for someone else :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload