我目前正在ListView中创建一个具有多个ListViewItem的应用程序,它对双击事件做出反应。但是,我需要知道在ListView上双击时会双击哪个项目。
当双击UWP中的列表视图时,它不会在触发双击事件之前选择该项目。因此,我无法通过myListView.SelectedItem获取该项。除非我遗漏了某些内容,否则UWP似乎并不支持命中测试。
思想?
答案 0 :(得分:2)
我通过以下方式解决了我的问题:
我将Listviewitem内部网格上的IsHitTestVisible属性设置为false(仅当列表视图中有控件时才适用)。
我订阅了ListView的双击事件
在事件触发后,我使用DoubleTappedRoutedEventArgs来获取原始源并将其转换为Listviewitem(或者在我的情况下,我的用户控件)。
以下是代码:
自定义Listviewitem: XAML
<ListViewItem x:Name="ListBoxItemContainer" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Padding="0,0">
<Grid x:Name="MainGrid" HorizontalAlignment="Stretch" IsHitTestVisible="False">
<!-- Your controls here -->
</Grid>
</ListViewItem>
列表视图代码:
private async void MainListView_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
{
var test = (ListViewItem)MainListView.SelectedItem;
// Test is your reference to the list view item that was clicked
}
注意:请务必订阅列表视图的双击事件。
答案 1 :(得分:0)
试试这个:
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ListView.SelectedIndex == -1) return;
_previousIndex = ListView.SelectedIndex;
if (_previousIndex == ListView.SelectedIndex)
{
//double-click occured
_previousIndex = -2;
}
ListView.SelectedIndex = -1;
}