检测发生 rightTapped 事件的 ListViewitem [英] Detect ListViewitem where rightTapped event happened

查看:21
本文介绍了检测发生 rightTapped 事件的 ListViewitem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 RightTappedRoutedEventArgs e 获取 ListItem 而没有 SelectionChanged 事件和相关来源,因为这个 ListViewSelectionMode="none".如果 SelectionMode="none" 无法做到这一点,它是否适用于其他选择类型,但仍然没有选择更改事件?

Is there any way to get the ListItem from RightTappedRoutedEventArgs e without SelectionChanged event and related sources because this ListView have SelectionMode="none". If this is not possible with SelectionMode="none", will it be available with other selection types, but still without selection change event?

下面的项目 xaml 模板.

Item xaml template below.

<ListView.ItemTemplate>
 <DataTemplate>
   <Grid Height="56" Width="300">
     <Image .../>
    <TextBlock .../>
   </Grid>
 </DataTemplate>
</ListView.ItemTemplate>

由于一些实验,我对 ListView(具有当前未使用的功能)进行了子类化,并通过处理 RightTapped 对 ListViewItem 进行了子类化.也许有什么方法可以将 this 附加到事件?我认为将其作为选择结果存储在子类 ListView 中是一种不好的行为.

Due to some experiments, I have subclassed ListView (with currently unused functionality) and also subclassed ListViewItem with handling RightTapped. Maybe there is any way to attach this to event? Storing this as selection result in subclassed ListView is a not good behavior I think.

推荐答案

<ListView.ItemTemplate>
 <DataTemplate>
   <Grid Height="56" Width="300" IsHitTestVisible="False">
...

所以现在我总是将 Border 视为原始发件人.之前是 TextBoxImage.

So now I see always Border as original sender. It was TextBox or Image before.

然后在:

private void itemsListBoxRightTapped( object sender, RightTappedRoutedEventArgs e )
{
  Border clickBorder = e.OriginalSource as Border;
  if ( clickBorder != null )
  {
     MyItemType selectedItem = clickBorder.DataContext as MyItemType;
   ...

中提琴!点按项目.

现在完成了 ListView 的正确上下文菜单;)

Now correct context menu for ListView is done ;)

更新:Windows 通用应用具有 ListViewItemPresenter 而不是 Border.

Update: Windows Universal apps have ListViewItemPresenter instead of Border.

这篇关于检测发生 rightTapped 事件的 ListViewitem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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