WPF - MVVM: ComboBox value after SelectionChanged

前端 未结 2 668
轮回少年
轮回少年 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:27

    Why not do it the simpler way

    
    

    In your ViewModel declare the combo box items and use a property "Source" to return it to the view

    List _source = new List{"Item 1", "Item 2", "Item 3"};
    public List Source 
    { 
        get { return _source; } 
    }
    

    Then define one property which holds the selected item

    string _theSelectedItem = null;
    public string TheSelectedItem 
    { 
        get { return _theSelectedItem; } 
        set { _theSelectedItem = value; } // NotifyPropertyChanged
    }
    

    Also don't forget to implement the INotifyPropertyChanged interface while setting the _source

提交回复
热议问题