【问题标题】:Xamarin Forms Picker CascadingXamarin 表单选择器级联
【发布时间】:2018-02-02 03:26:48
【问题描述】:

我参与了一个项目,遇到以下情况: 我有一个选择器(下拉菜单),它需要根据第一个选择器(级联)中的第一个选定项目显示第二个选择器。值是动态的(来自 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 的相应值)。顺便说一句,我会很感激任何其他方法,因为我有兴趣获得预期的结果。 谢谢你们的支持。 我为此工作了好几个小时,但在互联网上找不到任何值得的东西。

【问题讨论】:

  • 我解决了一个类似的问题,我的一个涉及到你必须先选择一所房子,然后它会用公寓填满另一个选择器,这是你想要的吗?

标签: c# visual-studio xaml xamarin xamarin.forms


【解决方案1】:

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

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

希望对你有帮助

【讨论】:

  • 嗨乔迪,我无法实现你的答案,这是 OnModeChosen 的全部代码吗?我还是卡住了
  • 在第 2 行,我调用了一个函数 GetRegions,这个函数你必须自己创建。在这里,您传递国家/地区的 idd 并使用该 id 将区域从您的 sqllite 数据库中取出,如果不是这样,您坚持什么?
  • ps刚刚注意到你禁用了你的picker2别忘了启用它,我将它添加到ym代码
  • 对不起,Jordy,你的代码对我不起作用,仍然卡住。
  • @JordyDieltjens 我的意思是没有在 SelectedIndexChanged 事件中手动设置内容。 IE。在 Xaml 中使用绑定。在代码隐藏期间动态添加事件绑定,对于简单的级联选择器解决方案来说似乎是一个滑坡。顺便说一句:我确实想出了如何做到这一点。以及通过 visibilityConverter 绑定绑定辅助/级联选择器的可见性。
猜你喜欢
  • 2021-06-29
  • 2018-02-01
  • 1970-01-01
  • 2015-12-04
  • 2021-04-18
  • 2021-12-14
  • 2019-06-20
  • 2021-01-04
  • 2021-05-24
相关资源
最近更新 更多