Alexander Ciesielski
Alexander Ciesielski

Reputation: 10834

Win8 ListView Tap does not select item

Hi so I'm doing a note taking app and Ive got it connected to my SQL Azure Database. The items get automatically populated through MobileServiceCollectionView. After tapping an item, a detailView should be opened to the right...

I dont know why but tapping an item does not select it, so I cant check for listView.SelectedItem in the noteListView_Tapped event.....

What is wrong with my listview?

<ListView x:Name="noteListView" Margin="20,0,0,0" Tapped="noteListView_Tapped">
 <ListView.ItemTemplate>
  <DataTemplate>
   <StackPanel Orientation="Horizontal" Margin="2">

     <CheckBox x:Name="CheckBoxComplete" IsChecked="{Binding Complete, Mode=TwoWay}" Checked="CheckBoxComplete_Checked" Padding="3"/>
     <TextBlock x:Name="TextBlockTodoItem" Text="{Binding Title}" Padding="3" />

   </StackPanel>
  </DataTemplate>
 </ListView.ItemTemplate>
</ListView>

And here the Tapped event.. It crashed on TodoItem t = (TodoItem)lv.SelectedItem;

So i added the if condition and now nothing happens except for the debug prints.

private void noteListView_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Debug.WriteLine("0");
        ListView lv = (ListView)sender;
        if (lv.Items.Count > 0)
        {
            if (lv.SelectedItems.Count > 0)
            {
                TodoItem t = (TodoItem)lv.SelectedItem;
                Debug.WriteLine("1");
                Debug.WriteLine(t.Title);
                try
                {
                    Location l = TodoItem.StringToLocation(t.LocationTaken);
                    Debug.WriteLine("2");
                    MapLayer.SetPosition(locationIcon, l);
                    map.SetView(l, 15.0f);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Map set position failed.: " + ex.Message);
                }
            }
        }
    }

So what do I have to change or how do I know which item has been clicked/selected?

Upvotes: 0

Views: 848

Answers (1)

J.B
J.B

Reputation: 1700

Why not just use the SelectionChanged event on the listview?

<ListView SelectionChanged="noteListView_SelectionChanged">

Then in your code behind

private void noteListView_SelectionChanged(object sender, RoutedEventArgs e)
{
    if((sender as ListView).Items.Count > 0)
    {
        .....
    }
}

I think (though I'm not sure) that your problem is that tapping the listView, and so the tapped event, is not the same as selecting an item in the list.

Upvotes: 1

Related Questions