user2745355
user2745355

Reputation:

How to show the SelectedItem of the ListPicker?

I dunno how to get/show/display the item that is selected now in the ListPicker. Is there anyway to do this ? If i run my below C# Code, the app breaks. I dunno why.

XAML:

<toolkit:ListPicker
    x:Name="categoriesListPicker"
    ItemsSource="{Binding CategoriesList}"
    DisplayMemberPath="Name"
    SelectionChanged="categoriesListPicker_SelectionChanged">

Code Behind:

    private void categoriesListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string selectedItem = categoriesListPicker.SelectedItem as string;
        MessageBox.Show(selectedItem);
    }

Upvotes: 0

Views: 136

Answers (1)

Bob Claerhout
Bob Claerhout

Reputation: 821

Wen the application fires, nothing is selected. I think your application is breaking on that. Before you get the selected item and parse it to a string you should check whether nothing is "null"

Try this:

private void categoriesListPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (categoriesListPicker.SelectedItem != null)
    {
                string selectedItem = categoriesListPicker.SelectedItem as string;
                MessageBox.Show(selectedItem);
    }
}

If this doesn't work, you should debug the application and try to find out which line breaks your applications. In addition, please provide the error that is thrown.

Upvotes: 1

Related Questions