samedi 9 mai 2015

Entity framework enum - binding to WPF - show selected item

I would like to ask for help with this task:

I have a Model Class with enum.

public enum ColorEnum { not set, black, white, red} public ColorEnum Color {get; set }

Now I am creating WPF window - easy form for edit a car and I would like to bind the enum items to a comboBox. I am using a MVVM architecture. I am not sure, what to put into EditCarViewModel - I don't know how to reference the enum from the Entity Framework.

protected string color;
    public string Color
    {
        get
        {
            return color;
        }
        set
        {
            color = value;
            NotifyPropertyChanged();
        }
    }

Into the EditCarWindow.xaml.cs I added this code and in the ComboBox I can choose the options (colors) from the enum:

public EditCarUserControl(int idCar=0)
        {
            InitializeComponent();
            this.DataContext = new EditCarViewModel(idCar);
            ComboBoxColor.ItemsSource = Enum.GetValues(typeof(Car.ColorEnum));
        }

But the problem is - I don't know how to let the color which is set in DB to be selected in the ComboBox. This doues not work.

<ComboBox x:Name="ComboBoxColor" Grid.Column="1" Grid.Row="4" Margin="3" SelectedItem="{Binding Color, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                 />

I guess the problem is in the ViewModel - Of course I can't have the fuel as a string - but what should I do?

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire