0

I found some topics "How to bind a combobox from enum list", but my problem is that I try to use MVVM arhitecture and let my View (xaml) clear as you see below:

part of View (xaml.cs):

public partial class GreenCertificatesStockForm : Erp.Core.Wpf.BaseWindow
    {
        private Models.GreenCertificatesGroupModel model;
        public GreenCertificatesStockForm()
        {
            model = new Models.GreenCertificatesGroupModel();

            this.DataContext = model;

            InitializeComponent();

            model.LoadForm(); // propose some dates for my form
            model.RequestClose += () => { Close(); };
        }
    }

part of View (xaml) my RadComboBox:

<telerik:RadComboBox Name="certificatesTypeRadComboBox"
                      Margin="5 2 0 2"  Width="150"
                      SelectedValue="{Binding CertificatesTypeEnum , Mode=TwoWay, 
                                                ValidatesOnDataErrors=True, 
                                                ValidatesOnExceptions=True, 
                                                NotifyOnValidationError=True}"
                      ItemSource="{Binding }"
                      SelectedItem="{Binding }"
                      telerik:StyleManager.Theme="Office_Blue" BorderBrush="#FF707070" Background="#FFDDDDDD" 
                                >

            </telerik:RadComboBox>

So, my Enum list is created in my ViewModel (class.cs) as:

public enum CertificatesTypeEnum {
            Estimat = 1,
            Calculat = 2,
            Facturat = 3
            }

I need to display in my RadComboBox all Values of the Enum and by selectedValue to save the specific Key of selection in DB (this with a parameter). How can I display the values from ViewModel into ComboBox (View)?

I know can do something like :

var items = Enum.GetValues(typeof(CertificatesTypeEnum)).Cast<CertificatesTypeEnum>().Select(i => new ComboboxItem()
        { Text = Enum.GetName(typeof(gender), i), Value = (int)i}).ToArray<ComboboxItem>();
        //Add the items to your combobox (given that it's called comboBox1)
        RadComboBoxName.Items.AddRange(items);

but this must be made in xaml.cs file (and I don't want this solution), because in ViewModel the combobox are not recognised and will be not found.

In short : display Values of Enum list from ViewModel class in xaml file.

AlleXyS
  • 2,476
  • 2
  • 17
  • 37
  • See [this answer](https://stackoverflow.com/a/3987099/1136211) for how to bind the ItemsSource to a collection of all enum values. In addition, your view model should have a property for the selected enum value, to which you would bind the SelectedItem property. – Clemens Jan 30 '18 at 09:54

1 Answers1

1

Why don't you just call the Enum.GetValues method in the view model? This is MVVM:

public class GreenCertificatesGroupModel
{
    public IEnumerable<CertificatesTypeEnum> EnumValues
    {
        get
        {
            var list = Enum.GetValues(typeof(CertificatesTypeEnum)).Cast<CertificatesTypeEnum>();
            return list;
        }

    }

    private CertificatesTypeEnum _selectedItem;

    public CertificatesTypeEnum SelectedItem
    {
        get { return _selectedItem; }
        set { _selectedItem = value; }
    }
}

XAML:

<telerik:RadComboBox ItemsSource="{Binding EnumValues}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" />
AlleXyS
  • 2,476
  • 2
  • 17
  • 37
mm8
  • 163,881
  • 10
  • 57
  • 88
  • this work, values appear on my combobox, but how can I save the specific Id of selected value? Because in the List, I save only values (Enum.GetValues). I tried something like int Id = (int)CertificatesTypeEnum.SelectedItem, but doesn't work – AlleXyS Jan 30 '18 at 12:37
  • In the view model, you would do: `int x = (int)_selectedItem;` – mm8 Jan 30 '18 at 13:01
  • in xaml is a problem. I need to use SelectedValue="{Binding SelectedItem}" to return the value, this is ok, I can save the "Id" in my database, but the problem is if I use SelectedValue or SelectedItem, after select one item from combobox, all items from list dissapear, are hidden, but the values are there. If I don't use one of these 2 properties, can't use the value in code, every time return 0. – AlleXyS Jan 30 '18 at 13:28
  • You should use SelectedItem and bind to an enum property as per my example...don't involve SelectedValue. – mm8 Jan 30 '18 at 13:31
  • Done. But it's a little strange. First, use SelectItem="{Binding SelectedItem, Mode=TwoWay}", then in my datatable I update the column's value: dr["Type"] = selectedItem; What is strange, property selectedItem have a string value [example "estimate"], but to dr["Type"] which is int, transfer the key of "estimate" value, from Enum list. I don't know how this is possible, maybe due of EnumValues (public IEnumerable EnumValues), the list return the Key of selected value. – AlleXyS Jan 30 '18 at 14:06
  • Please ask a new question if you have another issue. This question was about how to display enum values in a RadComboBox using MVVM and I believe it has been answered now. – mm8 Jan 30 '18 at 14:08