开发者

Using MVVM how can I find out if a ComboBox has changed the selected value?

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 unde开发者_如何学编程rstand it. I used a value called CollectionViewSource that it does not explain what is or where it is obtained.)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜