How to handle ListPicker SelectionChanged event properly?

1.7k views Asked by At

Good day, everybody. I have the following problem:

I have a listpicker with the x:Name="Backgroundlist" declared in XAML and it works fine.

Its items are declared as follows:

public MainPage()
{
    InitializeComponent();
    Backgroundlist.Items.Add("photo");
    Backgroundlist.Items.Add("Bing");               
}

However, the following code does not do its job (i.e. it doesn't show the MessageBox):

private void Backgroundlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (Backgroundlist.SelectedItem.Equals("photo"))
    {   
        MessageBox.Show("photo");
    }
    if (Backgroundlist.SelectedItem.Equals("Bing"))
    {
        MessageBox.Show("Bing");
    }
}

What seems to be the problem? Thanks!

P.S. I do not get an exception

1

There are 1 answers

0
Jan Guardian On

Ok. So here's the deal. As well I had to declare a SelectionChanged="Picker" in XAML. Here's the full code that works:

<toolkit:ListPicker x:Name="Backgroundlist" Header="Background" SelectionChanged="Picker" ExpansionMode="FullscreenOnly" />

public MainPage()
        {
            InitializeComponent();
            Backgroundlist.Items.Add("photo");
            Backgroundlist.Items.Add("Bing");
        }

        private void Picker(object sender, SelectionChangedEventArgs e)
        {
            var picker = sender as ListPicker;
            MessageBox.Show(picker.SelectedItem.ToString());
        }

Everything turned out to be very primitive in the end)