I have a model like this ( City.cs in Modle folder)
public class City
{
public int Key { get; set; }
public string Name { get; set; }
public List<string> Regions { get; set; }
public City()
{
Regions = Regions ?? new List<string>();
}
}
Data like this (ViewModel.cs in ViewModel folder)
public List<City> GetCities()
{
var c = new List<City>()
{
new City() { Key = 1, Name = "Keelung", Regions = { "CenterK", "EastK", "NorthK", "WestK", "SouthK" } },
new City() { Key = 2, Name = "Hsinchu", Regions = { "HEast", "HNorth", "HCenter" } },
new City() { Key = 3, Name = "Chiayi", Regions = { "CEast", "CWest" } }
};
return c;
}
In my view.xaml is
Picker x:Name="CityPicker"
Title= "City"
ItemsSource="{Binding CitiesList}"
ItemDisplayBinding="{Binding Name}"
SelectedItem="{Binding SelectedCity}"/
Picker x:Name="RegionPicker"
Title="Region"
BindingContext="{x:Reference CityPicker}"
ItemsSource="{Binding MyRegion}"
ItemDisplayBinding="{Binding Items}"
In my viewmodel.cs is
public City SelectedCity
{
get { return _selectedCity; }
set
{
if (_selectedCity != value)
{
_selectedCity = value;
var _myregion = GetCities().Where(x => x.Name == _selectedCity.Name).Select(s => s.Regions);
OnPropertyChanged();
}
}
}
List<string> _myregion;
public List<string> MyRegion
{
get { return _myregion; }
set
{
if (_myregion != value)
{
_myregion = value;
OnPropertyChanged();
}
}
}
public ClinicSearchStartViewModel()
{
try
{
ClinicListResponses = new ObservableRangeCollection<ClinicListResponse>();
Title = "Search";
CitiesList = GetCities().OrderBy(k => k.Key).ToList();
GetClinicListResponsesCommand = new Command<ClinicSearchRequest>(async (clinicsearchrequest)
=> await GetClinicListResponsesAsync(clinicsearchrequest));
}
catch (Exception ex)
{
throw;
}
}
How can write a code to make RegionPicker ItemDisplayBinding show CityPicker SelectedItem related Region
for example
CityPicker Select Keelung then click RegionPicker will show { "CenterK", "EastK", "NorthK", "WestK", "SouthK" }
Thanks for Reading````