Xamarin.Forms "Picker" Binding Problem

Mohammad Radawan 106 Reputation points
2022-02-27T07:58:53.857+00:00

I've designed an application to collect data from users about their addresses (city and district / province). In my database I have two tables, one for cities and another for their corresponding districts. In my XF app, I'm using MVVM to connect to the database, I have two Pickers one for cities, one for districts, and I've populated data in the cities Picker in the constructor. When I choose a city the other picker is populated with the related districts, but when I choose a district, and for some reason, I get back to change the city, the districts Picker is never filled with any data.

Code for getting the GUID of the selected city, then calling the method for getting the regions
178149-image-003.png
Method for getting the regions based on the city GUID
178106-image-004.png

        private string CityGuide;  
        private Category selectedCity;  
        public Category SelectedCity  
        {  
            get => selectedCity;  
            set  
            {  
                SetValue(ref selectedCity, value);  
                CityGuide = selectedCity.cardGuide;  
                //App.Current.MainPage.DisplayAlert("Current City GUID", CityGuide, "OK!");  
                LoadRegions();  
            }  
        }  

private async Task LoadRegions()  
        {  
            (List<Category> regionsData, string regionsResponse) = await new   
             CategoriesJsonService().LoadRegionsByCity(Guid.Parse(CityGuide));  
            if (regionsResponse != "Success!")  
            {  
                //error message  
                await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(new MessageView("حدث خطأ اثناء عملية الاتصال بمزود البيانات"));  
                return;  
            }  
            Regions = regionsData;  
        }  
Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2022-02-28T05:36:41.963+00:00

    Hello,​

    when I choose a district, and for some reason, I get back to change the city, the districts Picker is never filled with any data.

    If you reselect the city, the district picker do not update, Am I right?

    If so, please set ObservableCollection for your Regions property. Because ObservableCollection implement the INotifyPropertyChanged interface, when you update the ObservableCollection, picker's ItemsSource will be change at runtime.

    And change your LoadRegions method like following code. when you get the new Rigons, clean it and add the new Rigons.

       public ObservableCollection<Category> Regions { get; set; } = new ObservableCollection<Category>();  
               private async Task LoadRegions()  
               {  
                   (List<Category> regionsData, string regionsResponse) = await new  
                    CategoriesJsonService().LoadRegionsByCity(Guid.Parse(CityGuide));  
                   if (regionsResponse != "Success!")  
                   {  
                       //error message  
                       await Rg.Plugins.Popup.Services.PopupNavigation.Instance.PushAsync(new MessageView("حدث خطأ اثناء عملية الاتصال بمزود البيانات"));  
                       return;  
                   }  
                   Regions.Clear();  
                   foreach (var item in regionsData)  
                   {  
                       Regions.Add(item);  
                   }  
                 //  Regions = regionsData;  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.