2
votes

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
the following code does not do its job do you get an exception?PoweredByOrange
Dear PoweredByOrange, Thanks for the prompt reply) No, I do not get an exception.Jan Guardian
What is SelectedItem's type when SelectionChanged is called?PoweredByOrange
Sure. Set a break point on line if (Backgroundlist.SelectedItem.Equals("photo")). Then hover your mouse over SelectedItem and tell me what it says.PoweredByOrange
So does it have the value "photo" or "Bing"? Have you tried the SelectedValue property?PoweredByOrange

1 Answers

2
votes

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)