Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get clicked item in ListView

Tags:

c#

windows-8

xaml

This should be a trivial Task but I can't find how to do it. I want to listen to a click on an item in a listview, get the corresponding model object, and launch a new screen.

This is the XAML for the ListView:

<ListView x:Name="ItemListView" 
    ItemTemplate="{StaticResource StoreFrontTileTemplate}"
    ItemContainerStyle="{StaticResource StoreFrontLVTileStyle}"
    Margin="0" VerticalAlignment="Top" ItemClick="MyClick" Tapped="MyTap"/>

And MyClick, MyTap:

private void MyClick(object sender, ItemClickEventArgs e)
{
    Debug.WriteLine("Click!");
}

private void MyTap(object sender, TappedRoutedEventArgs e)
{
    Debug.WriteLine("TAp!!" + sender.ToString() + "--" + e.ToString());
}

The method to navigate to the new screen:

this.Frame.Navigate(typeof(SecondScreen));

It works, but I need the model object of the clicked item and pass it as a Parameter to the second screen.

But MyClick is never called, and MyTap doesn't give me any Information about the clicked item. "sender" is the ListView.

I downloaded these exaples:

http://code.msdn.microsoft.com/windowsapps/XAML-ListView-sample-pack-0ec6b60f

But it doesn't contain what I need, there's a master / detail view, but it works with bindings and what I want to do is launch a complete new screen.

Note: I'm a noob in Windows development and orienting to the usual way to do it in Android or IOS where you implement a callback with the position of the clicked element. No idea about the right way to do it in Windows 8.

like image 836
User Avatar asked Nov 10 '12 22:11

User


People also ask

How to get data from ListView by clicking item on ListView?

Try this: my_listview. setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { } });

How can you react to click events on an item of a ListView?

Place an empty linearlayout below the listview by setting an appropriate height to the listview. Place an onClick() method to that linear layout. That must do it. Save this answer.

How do I get the selected list of items?

To get which item was selected, there is a method of the ListView called getItemAtPosition. Add this line to your OnItemClick method: String itemValue = (String) theListView. getItemAtPosition( position );


1 Answers

You can use the SelectionChanged event:

<ListView x:Name="ItemListView" SelectionChanged="MySelectionChanged" />

And you can get the selected/deseleted items from the SelectionChangedEventArgs e.g.:

private void MySelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Debug.WriteLine("Selected: {0}", e.AddedItems[0]);
}

Or if you don't need the selection functionality and what to use the ItemClick="MyClick" you need to set IsItemClickEnabled to true:

Gets or sets a value that indicates whether items in the view raise an ItemClick event in response to interaction.

<ListView x:Name="ItemListView" 
    ItemTemplate="{StaticResource StoreFrontTileTemplate}"
    ItemContainerStyle="{StaticResource StoreFrontLVTileStyle}"
    Margin="0" VerticalAlignment="Top" ItemClick="MyClick"  
    IsItemClickEnabled="bool" SelectionMode="None"/>

Note that in this case you also need to set the SelectionMode to None.

like image 152
nemesv Avatar answered Oct 05 '22 08:10

nemesv