检测发生 rightTapped 事件的 ListViewitem [英] Detect ListViewitem where rightTapped event happened
问题描述
有没有办法从 RightTappedRoutedEventArgs e
获取 ListItem
而没有 SelectionChanged
事件和相关来源,因为这个 ListView
有 SelectionMode="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
视为原始发件人.之前是 TextBox
或 Image
.
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屋!