Search code examples
c#windows-phone-8listpicker

WP8 ListPicker SelectionChanged fired at the beginning


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;
}

Solution

  • 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;