views:

43

answers:

2

I have a view that has 2 combo boxes (Call them ParentTypeCombo and ChildTypeCombo). Both of these combo boxes have the same backing drop down list (call it WorkItemTypes).

Using MVVM how can I know when the value was changed for these combo boxes? I have bound them to properties (Call them ParentType and ChildType). But as I recall, my setter will not be called by WPF.

I don't want to just go off the event on the combo box because that will go in the code behind, not the View Model.

(I saw an example using an ObservableCollection. But I confess I did not understand it. I used a value called CollectionViewSource that it does not explain what is or where it is obtained.)

A: 

Set the ComboBox IsSyncronyzedWithCurrentItem property to true, than on your vm, call this CollectionViewSource.GetDefualtView([your workitem types]), the return type is ICollectionView or something similar, and it has a current changed event.

Chen Kinnrot
Will that work with one collection (WorkitemTypes) backing two ComboBoxes?
Vaccano
Don't know for sure check it...
Chen Kinnrot
+1  A: 

Just bind the SelectedItem to a property in the ViewModel for both parent and child

<ComboBox SelectedItem="{Binding ParentSelectedItem}" ... />

// VM

public WorkItemType ParentSelectedItem
{
    get { return _parentSelectedItem; }
    set
    {
        if(value != _parentSelectedItem)
        {
            //HERE you know it has changed value.
            _parentSelectedItem = value;
            RaisePropertyChanged("ParentSelectedItem");
        }
    }
  }

Also you can have only one collection on the view model and bind them to both combo boxes.

aqwert
When WPF changes the value will it call my setter? I did not think so. (But I am not 100% on binding)
Vaccano
Yes, by default binding is two way so it will call the setter. Try it with a simple app and you will see
aqwert