Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - MVVM - ComboBox SelectedItem

I have ViewModel(implemented INotifyPropertyChanged) in the background and class Category which has only one property of type string. My ComboBox SelectedItem is bind to an instance of a Category. When i change the value of instance, SelectedItem is not being updated and Combobox is not changed.

EDIT: code

Combobox:

<ComboBox x:Name="categoryComboBox" Grid.Column="1"  Grid.Row="3" Grid.ColumnSpan="2" 
          Margin="10" ItemsSource="{Binding Categories}"
          DisplayMemberPath="Name" SelectedValue="{Binding NodeCategory, Mode=TwoWay}"/>

Property:

private Category _NodeCategory;
public Category NodeCategory
{
    get
    {
        return _NodeCategory;
    }
    set
    {
        _NodeCategory = value;
        OnPropertyChanged("NodeCategory");
    }
}

[Serializable]
public class Category : INotifyPropertyChanged
{
    private string _Name;
    [XmlAttribute("Name")]
    public string Name
    {
        get
        {
            return _Name;
        }
        set
        {
            _Name = value;
            OnPropertyChanged("Name");
        }
    }

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    [field:NonSerialized]
    public event PropertyChangedEventHandler PropertyChanged;
}

and what I am trying is: when I set

NodeCategory = some_list_of_other_objects.Category;

to have that item selected in Combobox with appropriate DisplayMemberPath

like image 559
Bip Avatar asked Jun 16 '12 09:06

Bip


1 Answers

The category you are setting in this line -

NodeCategory = some_list_of_other_objects.Category;

and one present in your Categories collection(ItemsSource="{Binding Categories}") should be referring to same object. If they are not then SelectedItem won't work.

Solution 1 -

You can also try to use SelectedValuePath like this -

<ComboBox x:Name="categoryComboBox" 
          ItemsSource="{Binding Categories}"
          DisplayMemberPath="Name" 
          SelectedValuePath="Name" 
          SelectedValue="{Binding NodeCategory, Mode=TwoWay}" />

and in code you can do something like this -

private string _NodeCategory;
public string NodeCategory
{
    get
    {
        return _NodeCategory;
    }
    set
    {
        _NodeCategory = value;
        OnPropertyChanged("NodeCategory");
    }
}

and set selected item like this -

NodeCategory = some_list_of_other_objects.Category.Name;

and use selected value like this -

Category selectedCategory = 
   some_list_of_other_objects.FirstOrDefault(cat=> cat.Name == NodeCategory);

or

Category selectedCategory = 
   Categories.FirstOrDefault(cat=> cat.Name == NodeCategory);

Solution 2 -

Another possible solution can be -

NodeCategory = 
  Categories.FirstOrDefault(cat=> cat.Name == some_list_of_other_objects.Category.Name);

this way your NodeCategory property will have the reference of an object in Categories collection and SelectedItem will work.

like image 171
akjoshi Avatar answered Sep 17 '22 23:09

akjoshi