Everest
Everest

Reputation: 580

Raise PropertyChanged when selected item is changed in Combobox in Silverlight MVVM

How to change a property change in combobox when item that is selected in the combobox is changed. Actually what i want is when a value selected in one combobox is changed then based on the value selected other combox is to be filled. I have implenemted INotifyProperty interface even then when you select a different value the set block of property is not getting executed.

Let me elaborate a bit more with a different example :- There are two combobox and a textblock, both combobox contain the cities name, so when a user selects the city in the second combobox the a method should be called which will give the distance between the two cities in the textblock, and i am doing this using MVVM. The problem is that i am not able to invoke the set block of second combobox(from where i'll call the method which will give the distance).

Upvotes: 1

Views: 3374

Answers (1)

Jackson Pope
Jackson Pope

Reputation: 14640

You need to bind the SelectedItem to a property in the code behind or ViewModel if you're using MVVM, ensuring that the binding is two way and the property implements INotifyPropertyChanged:

<ComboBox Name="ComboBox1" ItemsSource="{Binding Items}" 
          SelectedItem="{Binding Path=SelectedItem,Mode=TwoWay}" 
          DisplayMemberPath="TextProp" >

public Collection<ItemClass> Items
{
     get
     {
          return _items;
     }
     private set
     {
           if (value != _items)
           {
                _items = value;
                RaisePropertyChanged("Items");
           }
     }
 }

 public ItemClass SelectedItem
 {
      get
      {
          return _selectedItem;
      }
      set
      {
           if (value != _selectedItem)
           {
                _selectedItem = value;
                RaisePropertyChanged("SelectedItem");
           }
      }
  }

Upvotes: 1

Related Questions