Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 58056

Showing a picker after a button click

$
0
0

Hi,

I want to show a picker after a buttonclick to change the color of a boxview. How can I show/hide a picker in a button click event? This is my code:

public partial class MainPage : ContentPage
{
public MainPage ()
{
InitializeComponent ();
}

    Dictionary<string, Color> nameToColor = new Dictionary<string, Color>
    {
        { "Aqua", Color.Aqua },         { "Black", Color.Black },
        { "Blue", Color.Blue },         { "Fuschia", Color.Fuschia },
        { "Gray", Color.Gray },         { "Green", Color.Green },
        { "Lime", Color.Lime },         { "Maroon", Color.Maroon },
        { "Navy", Color.Navy },         { "Olive", Color.Olive },
        { "Purple", Color.Purple },     { "Red", Color.Red },
        { "Silver", Color.Silver },     { "Teal", Color.Teal },
        { "White", Color.White },       { "Yellow", Color.Yellow }
    };

    private void ChangeColorButton_Click(object sender, EventArgs e)
    {
        _showColorPicker ();
    }

    private void _showColorPicker() {
        Picker picker = new Picker
        {
            Title = "Color",
            VerticalOptions = LayoutOptions.CenterAndExpand
        };

        foreach (string colorName in nameToColor.Keys)
        {
            picker.Items.Add(colorName);
        }

        picker.SelectedIndexChanged += (sender, args) =>
        {
            if (picker.SelectedIndex > -1)
            {
                string colorName = picker.Items[picker.SelectedIndex];
                ColorBox.Color = nameToColor[colorName];
            }
        };
    }
}

Viewing all articles
Browse latest Browse all 58056

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>