Bidirectional binding of combobox to enumeration

I have a view related to my ViewModel using a DataTemplate like

<DataTemplate DataType="{x:Type ViewModels:ViewModel}">
    <Views:View />
</DataTemplate>

ViewModel includes a property ProcessOptionhaving a type MyEnum?, where MyEnum- is a user listing, which has three values Single, Multipleand All. I am trying to associate combobox with this property, so the following approach:

ViewModel has a property List<string>that

public List<string> Options
    {
        get 
        {
            return Enum.GetNames(typeof(MyEnum)).ToList();
        }
    }

to which I bind the property ItemsSource Combobox. Then, in addition to the property ProcessOption, ViewModel also has a property OptionName( string), which is designed to store the selected option name. ViewModel implements INotifyPropertyChanged, and both properties raise an event PropertyChangedin their setters. The binding I use is as follows:

<ComboBox ItemsSource="{Binding Options}"
    SelectedItem="{Binding OptionName}"
    SelectedValue="{Binding ProcessOption}"/>

. combobox , null, , ViewModel, .

, . ViewModel :

this.ProcessOption = objectFromDB.ProcessOption // this is the value restored from DB,        let say it is MyEnum.Multiple
this.OptionName = Options.First(x => x.Equals(Enum.GetName(typeof(MyEnum), objectFromDB.ProcessOption)));

, , , null Combobox, . - if (value == null) { return; } , View, Combobox - , .

, IsSynchronisedWithCurrentItem, , , , .

- ? , !

+3
1
<ComboBox ItemsSource="{Binding Options}"
    SelectedItem="{Binding OptionName}"
    SelectedValue="{Binding ProcessOption}"/>

, - TwoWay, , SelectedItem SelectedValue - / .

OptionName SelectedItem ProcessOption (TwoWay) IValueConverter, / string.

0

All Articles