简体   繁体   English

WP8 ListPicker SelectionChanged在开始时触发

[英]WP8 ListPicker SelectionChanged fired at the beginning

I have a ListPicker with a SelectionChanged Event. 我有一个带有SelectionChanged事件的ListPicker。 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. 我希望SelectionChange事件仅在更改选择内容时才触发,但是就我而言,每次加载页面时,它也会触发。

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#: C#:

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

基本上,通过默认,列表选择器或列表框或组合框或任何下拉列表具有特定的选定项目,即列表框的第零个项目。/..等。因此,在加载页面并将数据填充到列表框,否则将触发从-1的选择更改,即无数据状态到0选择更改为默认值:)

You can add the handler on the Loaded event handler in your code so you ensure it's only called after that. 您可以在代码中的Loaded事件处理程序上添加处理程序,以确保在此之后才调用该处理程序。

Update: 更新:

For example add this code in Loaded event of your page instead of XAML: 例如,将此代码添加到页面的Loaded事件而不是XAML中:

sightingTypesPicker.SelectionChanged += sightingTypesPicker_SelectionChanged;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM