gpt4 book ai didi

c# - Xamarin 表单选择器级联

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:52 25 4
gpt4 key购买 nike

我参与了一个项目,我遇到了以下情况:我有一个选择器(下拉菜单),它需要根据第一个选择器(级联)中的第一个选定项目显示第二个选择器。值是动态的(来自 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;
}

希望对你有帮助

关于c# - Xamarin 表单选择器级联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45858353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com