簡體   English   中英

Xamarin表單選擇器級聯

[英]Xamarin Forms Picker Cascading

我參與了一個陷入以下情況的項目:我有一個選擇器(下拉菜單),其中需要根據從第一個選擇器(層疊)中選擇的第一個項目顯示第二個選擇器。 值是動態的(來自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的相應值)。 順便說一句,我很感激任何其他方法,因為我對獲得預期的結果感興趣。 感謝您的支持。 我已經花了好幾個小時才能在網上找到任何值得的東西。

所以這就是我在項目中解決的方法:

  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;
    }

希望這可以幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM