如何在ListView中获得点击的项目 [英] How to get clicked item in ListView

查看:36
本文介绍了如何在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.

这是 ListView 的 XAML:

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"/>

还有 MyClick、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());
}

导航到新屏幕的方法:

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.

但是 MyClick 从来没有被调用过,而且 MyTap 没有给我关于被点击项目的任何信息.发件人"是 ListView.

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

我下载了这些示例:

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.

注意:我是 Windows 开发中的菜鸟,并且面向在 Android 或 IOS 中执行此操作的常用方法,您可以在其中使用单击元素的位置实现回调.不知道在 Windows 8 中执行此操作的正确方法.

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.

推荐答案

您可以使用 SelectionChanged 事件:

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

并且您可以从 SelectionChangedEventArgs 获取选中/删除的项目,例如:

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]);
}

或者如果您不需要选择功能以及使用什么ItemClick="MyClick"您需要设置 IsItemClickEnabledtrue:

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

获取或设置一个值,该值指示视图中的项目是否引发 ItemClick 事件以响应交互.

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"/>

请注意,在这种情况下,您还需要将 SelectionMode 设置为 None.

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

这篇关于如何在ListView中获得点击的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆