UWP Listview 多选

时间:2021-08-11 15:13:08

最近在做一个项目的时候,用到了Listview,需要选择一个item,来进行删除。

但是当开启了

PullToRefreshListViewControl.IsMultiSelectCheckBoxEnabled = true;
PullToRefreshListViewControl.SelectionMode = ListViewSelectionMode.Multiple;

点击Item还是进入了Listview的Click事件。。。。。。

这里我们就需要用到一个小技巧了,顶一个一个全局变量 bool bMultiSelect = false;

最终效果

UWP Listview 多选

在Click事件中,判断是不是多选即可。

现在在xaml中定义三个button,实际工作不需要这么多,我这方便说明演示

一个是开启多选模式,一个是取消多选模式,最后是删除事件

<Button Content="MultiSelect" Click="MultiSelectClick"/>
<Button Content="CancelMultiSelect" Click="CancelMultiSelectClick"/>
<Button Content="DeleteSelected" Click="DeleteSelectedClick"/>

对应的后台code

     private void MultiSelectClick(object sender, RoutedEventArgs e)
{
bMultiSelect = true;
PullToRefreshListViewControl.IsMultiSelectCheckBoxEnabled = true;
PullToRefreshListViewControl.SelectionMode = ListViewSelectionMode.Multiple;
} private void CancelMultiSelectClick(object sender, RoutedEventArgs e)
{
bMultiSelect = false;
PullToRefreshListViewControl.IsMultiSelectCheckBoxEnabled = false;
PullToRefreshListViewControl.SelectionMode = ListViewSelectionMode.None;
} private async void DeleteSelectedClick(object sender, RoutedEventArgs e)
{
if (PullToRefreshListViewControl.SelectedItems.Count == )
{
Debug.WriteLine("No items selected");
return;
} for (int i = PullToRefreshListViewControl.SelectedItems.Count - ; i >= ; i--)
{
FaceSet fs = PullToRefreshListViewControl.SelectedItems[i] as FaceSet;
await FacePlusPlusHelper.DeletePersoninCloud(fs.FaceSetToken, fs.OuterId);
SampleItems.Remove(fs);
} if (SampleItems.Count == )
{
bMultiSelect = false;
PullToRefreshListViewControl.IsMultiSelectCheckBoxEnabled = false;
PullToRefreshListViewControl.SelectionMode = ListViewSelectionMode.None;
}
}

然后在Listview的点击事件中屏蔽一下即可

private async void PullToRefreshListViewControl_ItemClick(object sender, ItemClickEventArgs e)
{
  if(bMultiSelect)
    return;   //然后这里写单击item的代码咯
}

o, 对了,PullToRefreshListViewControl是Listview控件。