locked
How to add double tap event handler to all listviewitems RRS feed

  • Question

  • How do I add the same DoubleTapped event handler to each and every listviewitem entered?
    Friday, August 23, 2013 11:20 AM

Answers

  • This works:
    <ListView x:Name="MyListView">
      <ListView.ItemTemplate>
        <DataTemplate>
          <StackPanel x:Name="MyStackPanel" DoubleTapped="MySP_DoubleTapped" Background="Red" Width="300" Height="40">
            <TextBlock Text="{Binding}"/>
          </StackPanel>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      var MyData = new List<string>();
      MyData.Add("A");
      MyData.Add("B");
      MyData.Add("C");
      MyData.Add("D");
      MyData.Add("E");
      MyListView.ItemsSource = MyData;
    }
    
    private async void MySP_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
    {
      var MySP = sender as StackPanel;
      var MyTextBlock = MySP.Children[0] as TextBlock;
      var MyDialog = new MessageDialog(MyTextBlock.Text);
      await MyDialog.ShowAsync();
    }



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, August 27, 2013 3:23 PM
    Moderator

All replies

  • You can add DoubleTapped event to listview. Like:

    <ListView x:Name="list1" ItemsSource="{Binding abc}" Height="500" DoubleTapped="list1_DoubleTapped" >
    
    </Listview>

    May help you well.

    Friday, August 23, 2013 12:00 PM
  • But doesn't the event fire even if I double tap the area within the ListView that isn't an item?

    And, how do I access the item that's been double tapped from what you've suggested?

    Friday, August 23, 2013 12:02 PM
  • Hello eddy,

    You can double click on any item than this event is executed:

    private void list1_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
            {
                Debug.WriteLine("Item tapped");
            }

    If you are trying to single click that item doesn't selected you are to change that color. same as for doubleclick.

    Friday, August 23, 2013 12:38 PM
  • OK.

    So what would be the best way to implement a ListView where each item within that ListView must launch a DoubleTapped event handler? I also need the content of the ListViewItem as well.

    Friday, August 23, 2013 12:48 PM
  • Hello eddy,

    I am make one sample.

      <ListView x:Name="list1" ItemsSource="{Binding }" Height="437" DoubleTapped="list1_DoubleTapped" >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Image x:Name="img1" Source="{Binding path1}" Height="100" Width="100"/>
                                    <TextBlock x:Name="tbl1" Margin="5,0,0,0" Text="{Binding temp1}"/>
                                    <TextBlock x:Name="tbl2" Margin="5,0,0,0" Text="{Binding no}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

    Code:

      protected override void OnNavigatedTo(NavigationEventArgs e)
            {
     List<string> filetype = new List<string>();
                filetype.Add(".jpg");
                filetype.Add(".png");
    ObservableCollection<abc> abcd = new ObservableCollection<test2.abc>();
                var queryoption = new QueryOptions(CommonFileQuery.DefaultQuery, filetype);
                var query = subfolder.CreateFileQueryWithOptions(queryoption);
                IReadOnlyList<StorageFile> file1 = await query.GetFilesAsync();
                int i = 0;
                foreach (var file in file1)
                {
                    i++;
                    abcd.Add(new abc { path1 = new BitmapImage(new Uri(file.Path)), no = i.ToString(), temp1 = "text" + i.ToString() });
                }
                list1.DataContext = abcd;
    }
     private void list1_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
            {
                abc ab = (abc)list1.SelectedItem;
                Debug.WriteLine(ab.temp1);
                Debug.WriteLine(ab.no);
            }
    //abc Class
    public class abc
        {
            public BitmapImage path1 { get; set; }
            public string temp1 { get; set; }
            public string no { get; set; }
        }
    Saturday, August 24, 2013 4:25 AM
  • This works:
    <ListView x:Name="MyListView">
      <ListView.ItemTemplate>
        <DataTemplate>
          <StackPanel x:Name="MyStackPanel" DoubleTapped="MySP_DoubleTapped" Background="Red" Width="300" Height="40">
            <TextBlock Text="{Binding}"/>
          </StackPanel>
        </DataTemplate>
      </ListView.ItemTemplate>
    </ListView>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
      var MyData = new List<string>();
      MyData.Add("A");
      MyData.Add("B");
      MyData.Add("C");
      MyData.Add("D");
      MyData.Add("E");
      MyListView.ItemsSource = MyData;
    }
    
    private async void MySP_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
    {
      var MySP = sender as StackPanel;
      var MyTextBlock = MySP.Children[0] as TextBlock;
      var MyDialog = new MessageDialog(MyTextBlock.Text);
      await MyDialog.ShowAsync();
    }



    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, August 27, 2013 3:23 PM
    Moderator