2

I have a button on my screen and when I press the button I want a picker to show up with a list of items and when an item is chosen the text of the button should change to that and the picker should go away.

This is my code:

Picker picker = new Picker
    {
        Title = "What's in the slot?",
        VerticalOptions = LayoutOptions.CenterAndExpand
        //HorizontalOptions = LayoutOptions.Center 

    };

And the function which is called when the button is pressed:

private void Displaypickerview(int row, int column)
    {
        if (status == "filling board")
        {
            foreach (string text in pickerText)
            {
                picker.Items.Add(text);
            }
            foreach (string ore in oreLevels)
            {
                picker.Items.Add(ore);
            }


            picker.SelectedIndexChanged += (sender, args) =>
            {
                if (picker.SelectedIndex == -1)
                {

                }
                else
                {
                    //change value of cell and button
                    Picker picker = (Picker)sender;
                    int index = picker.SelectedIndex;

                    if (index < pickerText.Length)
                    {
                        board[row, column].Text = pickerText[index - 1];
                    }
                    else {
                        board[row, column].Text = oreLevels[index - 1 - pickerText.Length];
                    }
                }
            };
        }
        else if (status == "choosing item")
        {

        }

    }

But I don't know how i should render the picker view on the screen and remove it afterwards.

Update:

What is the eventhandler of the done button in the picture below.

Picker

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Cing
  • 806
  • 1
  • 11
  • 29

1 Answers1

5

You could add the picker to your Grid or whatever Panel you are using by adding it to the Children collection:

Grid.SetRow(picker, 0); //first row
Grid.SetColumn(picker, 0); //first column
grid.Children.Add(picker);

Similarly you remove it by removing it from the Children collection:

grid.Children.Remove(picker);
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I have a grid of button so wouldn't it remove the button on the first row and column – Cing Feb 12 '17 at 18:20
  • The Grid.SetRow/Grid.SetColumn methods sets the attached Grid.Row and Grid.Column properties of the element, i.e. it specifies on which row and column in the Grid the element (picker) will be located. And you remove the item that you pass to the Remove method. – mm8 Feb 12 '17 at 18:22
  • The remove function should be called when you press the done button but what is the code for the done button? – Cing Feb 12 '17 at 18:34
  • I have already showed you how to remove the picker from the Grid. Just do this in the button click event handler: button.Click += (ss, ee) => grid.Children.Remove(picker); – mm8 Feb 12 '17 at 19:07
  • It seems like you have already managed to add the picker then? Please ask a new question if you have a new issue. – mm8 Feb 12 '17 at 21:00
  • Oke I will do that – Cing Feb 12 '17 at 21:18
  • The new question: http://stackoverflow.com/questions/42193431/event-handler-of-the-done-button-of-a-picker – Cing Feb 12 '17 at 21:24