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.
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
.