XAML 需要帮助关于picker在xamarin

cs7cruho  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(169)

例如,当我选择一个国家,那么这个国家应该选择一个我在xaml或c#代码中提到的城市。
我代码:
公共分部类MainPage:如果您想创建一个新的列表,请点击这里。

public MainPage()
    {

        InitializeComponent();

        MainPicker.Items.Add("{USA");

///现在我想当我选择美国然后美国应该选择纽约怎么做?

}

    private void Button_Clicked(object sender, EventArgs e)
    {
        
  ///
       
        
    }

    private void MainPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var name = MainPicker.Items[MainPicker.SelectedIndex];
       
    }
}
kq0g1dla

kq0g1dla1#

创建一个类来为数据建模

public class CountryCity
{
   public string Country { get; set; }
   public string City { get; set; }

   public CountryCity(string Country, string City)
   {
      this.Country = Country;
      this.City = City;
   }
}

创建数据

List<CountryCity> myData = new List<CountryCity>();
myData.Add(new CountryCity("USA,"New York City"));
// add more data

将数据分配给选取器

MainPicker.ItemsSource = myData;

那么当选择了一个项目时

private void MainPicker_SelectedIndexChanged(object sender, EventArgs e)
{
    // this is the selected CountryCity object
    var item = (CountryCity)MainPicker.Items[MainPicker.SelectedIndex];
   
    // to get the individual values, use 
    // item.Country or item.City
}

相关问题