WPF Master/Detail data binding between 2 combobox

后端 未结 3 1563
太阳男子
太阳男子 2021-01-28 05:48

I have two combobox where Parent has to show the list of Countries and the child combo has to show a list of cities of the choosen country. The data is stored in a Diction

3条回答
  •  天命终不由人
    2021-01-28 06:33

    For the parent ComboBox, bind the SelectedItem to a property on your model:

    
    

    Where SomePropertyOnModel is of the same type as an Item in the Countries List.

    For the child ComboBox, everything should be the same:

    
    

    Side Note: You'll notice that I specifically added the ItemsSource binding to both the ComboBoxes.

    In the model, whenever the SomePropertyOnModel is set, update the CountriesCitiesList based on the value received, i.e.,:

    private string _somePropertyOnModel;
    public string SomePropertyOnModel 
    {
        get { return _somePropertyOnModel; }
        set 
        {
            _somePropertyOnModel = value;
            // call NotifyPropertyChanged
            UpdateCountriesCitiesList();
        }
    }
    
    private void UpdateCountriesCitiesList()
    {
        // set CountriesCitiesList based on the 
        // SomePropertyOnModel value
        // CountriesCitiesList should be an ObservableCollection and the values
        // should be cleared and then added.
        CountriesCitiesList.Clear();
        CountriesCitiesList.Add( "Springfield" );
    }
    

提交回复
热议问题