Your if
statement to execute code based on user selection looks fine, except value of Tag
in this case is a string so you should've compared it to another string ("1"
) instead of integer (1
).
The exception you got seems thrown when value of SelectedItem
is null. You can try to add simple checking at the beginning of the function to handle this condition properly and avoid NullReferenceException
:
private void Picker(object sender, SelectionChangedEventArgs e)
{
if(OptionSelection.SelectedItem == null)
{
//do some logic to handle null condition
//or simply exit the function if there is no logic to be done :
return;
}
var tag = ((ListPickerItem)OptionSelection.SelectedItem).Tag;
//value of Tag is a string according to your XAML
if (tag.Equals("1"))
{
MessageBox.Show("Item1 selected");
}
}