如何让ListView中单击项目 [英] How to get clicked item in ListView

查看:136
本文介绍了如何让ListView中单击项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是一个简单的任务,但我无法找到如何做到这一点。我想听听在ListView中的项目一点击,得到相应的模型对象,并推出一个新的屏幕。



这是XAML为ListView:

 < ListView控件X:NAME =ItemListView
的ItemTemplate ={StaticResource的StoreFrontTileTemplate}
ItemContainerStyle ={StaticResource的StoreFrontLVTileStyle}
保证金=0VerticalAlignment =顶级项目单击=MyClick认证螺纹=MyTap/>

和MyClick认证,MyTap:

 私人无效了MyClick(对象发件人,ItemClickEventArgs E)
{
的Debug.WriteLine(咔嚓!);
}

私人无效MyTap(对象发件人,TappedRoutedEventArgs E)
{
的Debug.WriteLine(点击!!+ sender.ToString()+ - - + e.ToString());
}



导航到新屏幕的方法:

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



它的工作原理,但我需要点击的项目的模型对象,并将其作为参数传递给第二个屏幕。



不过了MyClick不会被调用,而MyTap不给我谈单击项目的任何信息。 发件人是的ListView



我下载了这些exaples:



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



不过,这并不包含我需要什么,有一个主/详细信息视图,但它绑定的工作以及我想要做的就是推出完成新的屏幕。



请注意:我在Windows开发一个小白和定向到您实现与点击的元素的位置的回调通常的方式做到这一点在Android或IOS 。不知道要做到这一点在Windows 8以正确的方式。


解决方案

您可以使用的SelectionChanged 事件:

 < ListView的X:名称=ItemListView的SelectionChanged =MySelectionChanged/> 



,你可以得到所选/ deseleted项目从 SelectionChangedEventArgs 例如:

 私人无效MySelectionChanged(对象发件人,SelectionChangedEventArgs E)
{
调试.WriteLine(选择:{0},e.AddedItems [0]);
}



或者,如果你不需要选择功能以及如何使用项目单击=MyClick认证
你需要设置的 IsItemClickEnabled 真正




获取或设置一个值,指示在视图中的项目是否响应交互提高itemClick事件。




 < ListView的X:名称=ItemListView
的ItemTemplate ={StaticResource的StoreFrontTileTemplate}
ItemContainerStyle = {StaticResource的StoreFrontLVTileStyle}
保证金=0VerticalAlignment =顶级项目单击=MyClick认证
IsItemClickEnabled =布尔的SelectionMode =无/>

请注意,在这种情况下,你还需要设置的SelectionMode


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.

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

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