Jaster
Jaster

Reputation: 8581

Combobox not displaying changing values

I am stuck on a strange ComboBox issue.

I am using an ObjectDataProvider to feed a combobox:

    <ObjectDataProvider x:Key="foo" 
                    MethodName="GetNames" ObjectType="{x:Type sys:Enum}">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="local:FooEnum" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

Now I have a Class looking like this:

public class SomeClass : NotifyHelper
{
    private FooEnum _value;

    public FooEnum Value
    {
        get { return _value; }
        set
        {
            _value = value;
            OnPropertyChanged("Value");
        }
    }
}

A collection of SomeClass is bound to an ItemsControl with a combobox template

        <ItemsControl ItemsSource="{Binding Items}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <ComboBox ItemsSource="{Binding Source={StaticResource foo}}" SelectedItem="{Binding Value}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

The ViewModel:

public class VM : NotifyHelper
{
    public VM ()
    {
        Items = new List<SomeClass>();
        Items.Add(new SomeClass{Value = Foo.X});
    }

    public List<SomeClass> Items {get; private set; }
}

My issues:
- Initialy the comboboxes have no item selected
- when I change the value in the viewmodel instead in the view, the value is not updated in the comboboxes.

Changing the binding of the ComboBox SelectedItem to TwoWay and OnPropertyChange has no influence!

What am I missing?

Upvotes: 0

Views: 520

Answers (2)

RockWorld
RockWorld

Reputation: 1288

Set SelectedItem binding mode twoway

SelectedItem="{Binding Value, Mode=TwoWay}"

Yeah, GetVaues would do the job here.

Upvotes: 1

NamedJohnny
NamedJohnny

Reputation: 76

here is what i do to feed my comboBox with enum values with MVVM

in the constructor of my ViewModel I have this and I put my value (string) in a Resource file

private ObservableCollection<WritableKeyValuePair<int, string>> _traceDisplayList = null;
private WritableKeyValuePair<int, string> _selectedTraceType = new WritableKeyValuePair<int, string>();


public OptionsFilterViewModel()
{
       _traceDisplayList = new ObservableCollection<WritableKeyValuePair<int, string>>();
       _traceDisplayList.Add(new WritableKeyValuePair<int, string>((int)TraceDisplayEnum.All, ProjectResources.All));
       _traceDisplayList.Add(new WritableKeyValuePair<int, string>((int)TraceDisplayEnum.WithEx, ProjectResources.TraceException));
       _traceDisplayList.Add(new WritableKeyValuePair<int, string>((int)TraceDisplayEnum.ExOnly, ProjectResources.ExceptionOnly));

}

    /// <summary>
        /// Listes des types de traces
        /// </summary>
        public ObservableCollection<WritableKeyValuePair<int, string>> TraceDisplayList
        {
          get { return _traceDisplayList; }
          set
          {
            _traceDisplayList = value;
            RaisePropertyChanged<OptionsFilterViewModel>(x => x.TraceDisplayList);
          }
        }

        /// <summary>
        /// Type de trace sélectionné dans la liste
        /// </summary>
        public WritableKeyValuePair<int, string> SelectedTraceType
        {
          get
          {
            return _selectedTraceType;
          }
          set
          {
            _selectedTraceType = value;
            RaisePropertyChanged<OptionsFilterViewModel>(x => x.SelectedTraceType);
          }

        }

Upvotes: 0

Related Questions