Xamarin表单选择器级联

xmakbtuz  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(106)

我参与了一个项目,我陷入了以下情况:我有一个选择器(下拉),它需要显示第二个选择器的基础上,从第一个选择器(级联)第一个选定的项目。值是动态的(来自Sqlite表)。这里有我的代码:
XAML

Picker 1
    <Picker x:Name="picker" Title="Select Country" SelectedIndexChanged="OnModeChosen">
    Picker 2
    <Picker x:Name="picker2" Title="Select Regions" IsEnabled="False">

模型

public class Country
        {
          [PrimaryKey]
          Public int CountryId {get;set;}
          public string CountryName {get;set;}

          public Country(){}
        }

        public class Regions
        {
          [PrimaryKey]
          public int RegionsId {get; set;}
          public string RegionsName {get;set;}
          [ForeignKey(typeof(Country))]
          public int CountryId {get;set;}

          public Regions(){}
        }

//代码隐藏

private void OnModeChosen(object sender, EventArgs e)
    {
    Picker modePicker = (Picker)sender;
    var mode = picker.SelectedIndex;
    picker2.Items.Clear();

         switch(mode)
         {
           //This is the part I would like dynamic
         }
     }

我想处理程序“OnModeChosen”动态工作(从选择器1中选择的任何值将显示选择器2的相应值)。顺便说一句,我将赞赏任何其他方法,因为我对预期结果感兴趣。谢谢你们的支持。我花了几个小时在网上找不到任何有价值的东西。

e0uiprwp

e0uiprwp1#

这就是我在项目中解决它的方法:

private void OnModeChosen(object sender, EventArgs e)
    {
         Country country = ((Country)(picker.SelectedItem)) // get the country object from  the picker
          picker2.ItemsSource =  GetRegions(country.CountryId ) // call the function to get the regions for that country
          picker2.ItemDisplayBinding = New Binding("RegionsName")
          picker2.SelectedIndex = 0 // not sure why I did this, I think to make sure an item was selected
        stackpicker2.IsVisible = true // make the stack layout visible with the 2nd picker
        picker2.IsEnabled = true;
    }

希望这个能帮上忙

相关问题