Youness Benbiga
Youness Benbiga

Reputation: 37

Implementing combobox with command search

well i m having a problem when i use the method search i get only the textbox and not the combobox i m using mmvm here is my code:

in my constructor i have :

  CountryList = new FastObservableCollection<Country>(DummyWebservice.GetCountries());
     SearchCitizenCommand = new RelayCommand(SearchCitizen);

and for display countries and cities:

   private FastObservableCollection<City> citylist;
        public FastObservableCollection<City> CityList
        {
            get
            {
                return citylist;
            }
            set
            {
                Set(() => CityList, ref citylist, value);


            }
        }


        private FastObservableCollection<Country> countryList;
        public FastObservableCollection<Country> CountryList
        {
            get
            {
                return countryList;
            }
            set
            {
                Set(() => CountryList, ref countryList, value);

            }
        }
        private Country selectedcountry;
        public Country SelectedCountry
        {
            get
            {
                return selectedcountry;
            }

            set
            {
                Set(() => SelectedCountry, ref selectedcountry, value);
                OnPropertyChanged(() => SelectedCity);
                CityList = DummyWebservice.GetCitiesByCountryId(SelectedCountry.Id);

            }
        }
        private City selectedcity;
        public City SelectedCity
        {
            get
            {
                return selectedcity;
            }

            set
            {
                Set(() => SelectedCity, ref selectedcity, value);



            }
        }

and finnally in the method search i tried

    SelectedCountry = new Country();
    SelectedCountry.Name = citizen.Citizenship.Name;

in the view i got :

<ComboBox x:Name="txtBirthCountryPicker"
                                  Grid.Row="1"
                                  Grid.Column="1"
                                  HorizontalContentAlignment="Left"
                                  DisplayMemberPath="Name"
                                  ItemsSource="{Binding CountryList}"
                                  SelectedItem="{Binding SelectedCountry}" />

but im still getting it umpty

Upvotes: 0

Views: 35

Answers (2)

ibebbs
ibebbs

Reputation: 1993

The Combobox uses object equality to determine which of item in the ItemsSource is the selected item. As you're assigning a new instance of Country to the SelectedItem, it is unable to locate the country in the ItemsSource.

Try this instead:

SelectedCountry = CountryList.FirstOrDefault(c => c.Name.Equals(citizen.Citizenship.Name, StringComparison.CurrentCultureIgnoreCase);

Upvotes: 0

user6996876
user6996876

Reputation:

If you want to select an item in a combo, you need to choose the exact same object that is part of the ItemsSource.

SelectedCountry = CountryList.FirstOrDefault(
   x => x.Name == citizen.Citizenship.Name
)

Upvotes: 1

Related Questions