Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
348 views
in Technique[技术] by (71.8m points)

c# - How to get clicked item in ListView

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...