最近在做一個項目的時候,用到了Listview,需要選擇一個item,來進行删除。
但是當開啟了
PullToRefreshListViewControl.IsMultiSelectCheckBoxEnabled = true;
PullToRefreshListViewControl.SelectionMode = ListViewSelectionMode.Multiple;
點選Item還是進入了Listview的Click事件。。。。。。
這裡我們就需要用到一個小技巧了,頂一個一個全局變量 bool bMultiSelect = false;
最終效果
在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 == 0)
{
Debug.WriteLine("No items selected");
return;
}
for (int i = PullToRefreshListViewControl.SelectedItems.Count - 1; i >= 0; i--)
{
FaceSet fs = PullToRefreshListViewControl.SelectedItems[i] as FaceSet;
await FacePlusPlusHelper.DeletePersoninCloud(fs.FaceSetToken, fs.OuterId);
SampleItems.Remove(fs);
}
if (SampleItems.Count == 0)
{
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控件。
作者:貓叔Vincent
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接,否則保留追究法律責任的權利。