Reputation: 5480
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;
}
Upvotes: 1
Views: 2393
Reputation: 4899
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 :)
Upvotes: 3
Reputation: 2649
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;
Upvotes: 2