I have a LongListMultiSelector which is binded to an ObservableCollection. When I select all items in the list and try to delete as so it starts failing and returns false after 35 successful removes.
int failed = 0;
while ((App.ViewModel.Queue.Count - failed)> 0)
{
//get playlist viewmodel
bool success = App.ViewModel.Queue.Remove((ItemViewModel)QueueList.SelectedItems[0]);
if (!success)
failed++;
System.Diagnostics.Debug.WriteLine("DELETING " + success);
}
If I do not include the check for failed items it gets stuck in an endless loop. After the task completes, 35 of the 300 items are removed. If I re-run the task another 35 items get removed and the remaining fail...
My Observable collection is defined like so
public ObservableCollection<ItemViewModel> Queue { get; set; }
this.Queue = new ObservableCollection<ItemViewModel>();
In my xaml the MultiSelector is defined like this
<toolkit:LongListMultiSelector
x:Name="QueueList"
Margin="0,0,-12,0"
ItemsSource="{Binding Queue}"
LayoutMode="List"
SelectionChanged="QueueList_SelectionChanged">
How the Queue is populated (selected items from one list are copied to the Queue
foreach (ItemViewModel item in OriginalList.SelectedItems)
{
Boolean isQueued = false;
foreach (ItemViewModel queueItem in App.ViewModel.Queue)
{
if (queueItem.Equals(item))
{
isQueued = true;
break;
}
}
ItemViewModel newItem = new ItemViewModel();
newItem = item;
if (!isQueued)
{
App.ViewModel.Queue.Add(newItem);
for (int i = 0; i < ApplicationBar.MenuItems.Count; i++)
{
ApplicationBarMenuItem menuItem = (ApplicationBarMenuItem)ApplicationBar.MenuItems[i];
if (menuItem.Text.Equals("clear queue") && !menuItem.IsEnabled)
menuItem.IsEnabled = true;
}
}
}
OriginalList.SelectedItems.Clear();
DataSerializer.SaveQueue();
}
Iterate array of SelectedItems
and remove items one by one. Example:
public void delete()
{
while (Queue.SelectedItems.Count > 0)
{
Queue.ItemsSource.Remove(Queue.SelectedItems[0]);
}
}
You can also use the Clear method, which will iterate over the array and unselect the selected items.
private void Cancel_Selected(object sender, System.Windows.Input.GestureEventArgs e)
{
Queue.SelectedItems.Clear();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.