WPF - MVVM: ComboBox value after SelectionChanged

前端 未结 2 667
轮回少年
轮回少年 2021-02-06 02:47

I am new to C# and MVVM, and I\'ve spent all day trying to get the value of a ComboBox to my ViewModel on SelectionChanged. I have managed to figure it

2条回答
  •  离开以前
    2021-02-06 03:37

    If you just want to get notified when your current item changes, why not use tools that are already part of WPF instead of all these dependencies.

    First get the underlying view to your collection and use the CurrentChanged event on it.

    ComboBoxList = new ObservableCollection();
    var view = CollectionViewSource.GetDefaultView(ComboBoxList);
    view.MoveCurrentTo(ComboBoxList[0]);
    view.CurrentChanged += new EventHandler(ComboBoxCurrentChanged);
    

    In your xaml you just bind your collection and set IsSynchronizedWithCurrentItem to true.

    
    

提交回复
热议问题