Question

I have a ListPicker with a SelectionChanged Event. I want the SelectionChange event to only fire when I change the selection, obviously, but in my case, it is also firing whenever I load the page.

Code:

<toolkit:ListPicker x:Name="sightingTypesPicker" ItemsSource="{Binding sightingTypes, ElementName=this}" SelectionChanged="sightingTypesPicker_SelectionChanged">
   <toolkit:ListPicker.ItemTemplate>
      <DataTemplate>
         <StackPanel>
            <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
         </StackPanel>
      </DataTemplate>
   </toolkit:ListPicker.ItemTemplate>
   <toolkit:ListPicker.FullModeItemTemplate>
      <DataTemplate>
         <StackPanel>
            <TextBlock Text="{Binding Name}" FontSize="{StaticResource PhoneFontSizeLarge}"/>
            <TextBlock Visibility="Collapsed" Text="{Binding TypeId}" FontSize="{StaticResource PhoneFontSizeSmall}"/>
         </StackPanel>
      </DataTemplate>
   </toolkit:ListPicker.FullModeItemTemplate>
</toolkit:ListPicker>

C#:

private void sightingTypesPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    SightingType selecteditem = e.AddedItems[0] as SightingType;
    Sighting.Instance.TypeId = selecteditem.TypeID;
}
Was it helpful?

Solution 2

You can add the handler on the Loaded event handler in your code so you ensure it's only called after that.

Update:

For example add this code in Loaded event of your page instead of XAML:

sightingTypesPicker.SelectionChanged += sightingTypesPicker_SelectionChanged;

OTHER TIPS

Basically by defaut a list picker or a list box or a combo box or any drop down has a particular selected item and that is the zeroth item of the listbox./.. etc. So when the page is loading and data is being populated to the list boxes or else it fires the selection change from -1 ie no dat state to 0 selection changed to default value :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top