如何从StackPanel中的Tapped事件获取tapped项目

时间:2013-12-17 14:12:12

标签: c# xaml windows-phone-7 windows-phone-8 listpicker

我在应用程序页面中有一个ListPicker,但是当页面加载时,SelectionChanged事件会被多次调用。为了避免这种情况,我一直在关注我在此问ListPicker SelectionChanged Event Called Multiple Times During Navigation之前提出的问题,其中建议不是让ThemeListPicker_SelectionChanged在datatemplate中创建父堆栈面。',在StackPanel中创建一个名为{{1的tap事件并且'使用此点击stk_Tap来执行您的操作,此事件也会在每次调用选择被调用时调用,但是,它不会显示像选择更改事件那样的错误行为'

现在我已相应地调整了我的解决方案,但我不知道如何确定正在选择或当前选择了哪个ListPicker项。我还删除了ListPicker中的ListPicker SelectionChanged事件,因为我认为StackPanel可以获取该项,但我不确定这是否正确或如何执行此操作?

XAML

stk_Tap

XAML.CS

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="PickerItemTemplate">
                <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme"
                ItemTemplate="{StaticResource PickerItemTemplate}" 
                SelectionChanged="ThemeListPicker_SelectionChanged"/>

*编辑:我如何更新我的解决方案

private void ThemeListPicker_SelectionChanged(object sender,
                                          SelectionChangedEventArgs e)
{
   if(ThemeListPicker.SelectedIndex != -1)
   {
       var theme = (sender as ListPicker).SelectedItem;

       if (index == 0)
       {
          Settings.LightTheme.Value = true;
          MessageBox.Show("light");
       }
       else
       {
           Settings.LightTheme.Value = false;
           MessageBox.Show("dark");
       }
   }                            
} 

所以,即使我在进行修改后在代码中留下了ListPicker SelectionChanged事件,我也没有看到在加载/导航到页面时调用了两次事件,但我不知道如何获取当前选中的项目现在?

EDIT2 **

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Name="PickerItemTemplate"> 
           <StackPanel tap="stk_Tap"> 
                <TextBlock Text="{Binding Name}"/> 
            </StackPanel> 
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

<toolkit:ListPicker x:Name="ThemeListPicker" Header="Theme"
                ItemTemplate="{StaticResource PickerItemTemplate}" 
                />

1 个答案:

答案 0 :(得分:0)

无需为此类工作额外点击活动。

private void ThemeListPicker_SelectionChanged(object sender,
                                          SelectionChangedEventArgs e)
{
   if(ThemeListPicker.SelectedIndex==-1)
           return;
       var theme = (sender as ListPicker).SelectedItem;
       if (index == 0)
       {
          Settings.LightTheme.Value = true;
          MessageBox.Show("light");
       }
       else
       {
           Settings.LightTheme.Value = false;
           MessageBox.Show("dark");
       } 
       ThemeListPicker.SelectedIndex=-1                              
} 

ListPicker SelectionChanged事件在导航期间多次调用  对于上述问题如果我猜对了你在OnNavigatedTo事件上设置了listpicker的itemssource。所以用

修改你的onNavigatedTo方法
protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (e.NavigationMode != NavigationMode.Back)
            {
            // Your code goes here
            }
        }

//堆栈面板点击事件

private void stack_Tap(object sender, System.Windows.Input.GestureEventArgs e)
   {
    var selectedIrem = (Cast as your type)(sender as StackPanel).DataContext;
   }