在Xamarin表单中,如何使选择器中的选定选项在其他选择器中不可见?

时间:2019-05-21 13:25:15

标签: c# xaml xamarin.forms

enter image description here我有7个类似的选择器,但选项相同。现在用户可以选择     与picker1中的选项相同,它已经在picker2中选择。我想要在picker2中不可见的用户在picker中选择。

我已经用Google搜索过,但似乎没有人问过这个问题。

void drainxy1()
{
drainlocationPicker1.Items.Add("1");
drainlocationPicker1.Items.Add("2");
drainlocationPicker1.Items.Add("3");
drainlocationPicker1.Items.Add("4");       
}
void drainxy2()
{
drainlocationPicker2.Items.Add("1");
drainlocationPicker2.Items.Add("2");
drainlocationPicker2.Items.Add("3");
drainlocationPicker2.Items.Add("4");       
}
void drainxy3()
{
drainlocationPicker3.Items.Add("1");
drainlocationPicker3.Items.Add("2");
drainlocationPicker3.Items.Add("3");
drainlocationPicker3.Items.Add("4");       
}
void drainxy4()
{
drainlocationPicker4.Items.Add("1");
drainlocationPicker4.Items.Add("2");
drainlocationPicker4.Items.Add("3");
drainlocationPicker4.Items.Add("4");       
}
<Picker x:Name="drainlocationPicker1" Title="Drain Location" />
<Picker x:Name="drainlocationPicker2" Title="Drain Location" />
<Picker x:Name="drainlocationPicker3" Title="Drain Location" />
<Picker x:Name="drainlocationPicker4" Title="Drain Location" />
/*
Picker1= select from 1,2,3,4
user selects = 2
picker2= select from 1,3,4
user selects = 3
Picker3= select from 1,4
user selects = 1
picker4= select from 4
user selects = 4 */


void drain1Handle_SelectedIndexChanged(object sender, System.EventArgs e)
{           
drainlocationPicker2.Items.RemoveAt(drainlocationPicker1.SelectedIndex);            
drainlocationPicker3.Items.RemoveAt(drainlocationPicker1.SelectedIndex);              `   drainlocationPicker4.Items.RemoveAt(drainlocationPicker1.SelectedIndex);                
}

xamlfile

<Label x:Name="drain3Label" Text="Drain 3: " Grid.Column="0" 
HorizontalTextAlignment="Center" VerticalTextAlignment="Center" />
<Picker x:Name="drainlocationPicker3" Title="Drain Location" Grid.Column="1"
SelectedIndexChanged="drain3Handle_SelectedIndexChanged"/> 

1 个答案:

答案 0 :(得分:1)

我为此创建了一个简单的演示。 这里正在运行GIF。

enter image description here

您可以通过以下代码来实现。我创建了一个包含所有项目的列表。然后按照Jason的说明实现SelectedIndexChanged。注意:接下来,您需要自己判断用户的各种操作。

  public partial class MainPage : ContentPage
{
    List<string> list;
    public MainPage()
    {
        InitializeComponent();
        list = new List<string>();
        list.Add("1");
        list.Add("2");
        list.Add("3");
        list.Add("4");
        drainxy1();
        drainxy2();
        drainxy3();
        drainxy4();


        drainlocationPicker1.SelectedIndexChanged += DrainlocationPicker1_SelectedIndexChanged;
        drainlocationPicker2.SelectedIndexChanged += DrainlocationPicker2_SelectedIndexChanged;
        drainlocationPicker3.SelectedIndexChanged += DrainlocationPicker3_SelectedIndexChanged;
        drainlocationPicker4.SelectedIndexChanged += DrainlocationPicker4_SelectedIndexChanged;



    }

    private void DrainlocationPicker4_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void DrainlocationPicker3_SelectedIndexChanged(object sender, EventArgs e)
    {

        drainlocationPicker4.Items.RemoveAt(drainlocationPicker3.SelectedIndex);
    }

    private void DrainlocationPicker2_SelectedIndexChanged(object sender, EventArgs e)
    {

        drainlocationPicker3.Items.RemoveAt(drainlocationPicker2.SelectedIndex);
        drainlocationPicker4.Items.RemoveAt(drainlocationPicker2.SelectedIndex);
    }

    private void DrainlocationPicker1_SelectedIndexChanged(object sender, EventArgs e)
    {


        drainlocationPicker2.Items.RemoveAt(drainlocationPicker1.SelectedIndex);
        drainlocationPicker3.Items.RemoveAt(drainlocationPicker1.SelectedIndex);
        drainlocationPicker4.Items.RemoveAt(drainlocationPicker1.SelectedIndex);

    }

    void drainxy1()
    {
        foreach (var item in list)
        {
            drainlocationPicker1.Items.Add(item);
        }
    }
    void drainxy2()
    {

        foreach (var item in list)
        {
            drainlocationPicker2.Items.Add(item);
        }
    }
    void drainxy3()
    {

        foreach (var item in list)
        {
            drainlocationPicker3.Items.Add(item);
        }
    }
    void drainxy4()
    {

        foreach (var item in list)
        {
            drainlocationPicker4.Items.Add(item);
        }
    }
}