Hi,
Why can't we add an item to a bound ObservableCollection? it throws a dictionary exception
The only way to work around it is this kind of fudge:
var copy =
new
ObservableCollection<OrderItem>(Orders) {newOrder};
Orders =
new
ObservableCollection<OrderItem>(copy);
It worked fine before with a Xamarin Listview, when I refactored an existing app for Telerik RadList then this issue arose. Our WPF guys were saying they have the same issue with the WPF RadGrid.
Is this intended behavior or a bug?
Thanks,
Norman.
6 Answers, 1 is accepted
Thank you for contacting us.
I have tested several scenarios where a list view is bound to an observable collection and I could not reproduce the issue. I tried with collection of value type items and collection of equal items as I suspected this could be a problem, with and without item template. I have attached the test project. Could you please take a look and modify it to reproduce the issue.
I look forward to your reply.
Regards,
Rosy Topchiyska
Telerik

Hi Rosy,
I promise this didn't work in the last version I was using but having upgraded to the latest stable release I can delete from and clear the collection with no exceptions.
I did update your sample to use the same type of json dto's use and couldn't reproduce the issue either.
Thanks for your assistance,
Norman.

Hi Rosy,
The issue seems to be caused by a customrenderer in Android.
If you are using the renderer below the calling Collection.Clear() will throw an exception.
public
class
CustomRadListRenderer : ListViewRenderer
{
protected
override
SwipeExecuteBehavior CreateSwipeExecuteBehavior(ListViewDataSourceAdapter adapter)
{
var behavior =
base
.CreateSwipeExecuteBehavior(adapter);
behavior.AutoDissolve =
false
;
behavior.SwipeLimitStart = 0;
return
behavior;
}
}
The code is to stop the user swiping and revealing from the right. If I stop using the CustomRenderer then clear/add etc work as expected.
I'm guessing this approach has been deprecated for an newer approach but I'm not sure. Also could you advice how to stop this behavior in iOS?
The error is shown below:
System.Collections.Generic.KeyNotFoundExceptionThe given key was not present
in
the dictionary.
Raw
System.ThrowHelper.ThrowKeyNotFoundException()
System.Collections.Generic.Dictionary<TKey, TValue>.get_Item(TKey key)
at Telerik.XamarinForms.Common.XamarinToNativeControlExtensions.UpdateCollection[T,K] (Telerik.XamarinForms.Common.T nativeElement, Telerik.XamarinForms.Common.K xfЕlement, System.String propertyName, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) <0x897cd380 + 0x0005b>
in
<filename unknown>:0
Telerik.XamarinForms.DataControlsRenderer.Android.ListViewRenderer.ItemsSource_CollectionChanged(
object
sender, NotifyCollectionChangedEventArgs e)
Telerik.XamarinForms.DataControls.RadListView.RaiseItemsSourceCollectionChanged(
object
sender, NotifyCollectionChangedEventArgs e)
System.Collections.ObjectModel.ObservableCollection<T>.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
System.Collections.ObjectModel.ObservableCollection<T>.OnCollectionReset()
System.Collections.ObjectModel.ObservableCollection<T>.ClearItems()
System.Collections.ObjectModel.Collection<T>.Clear()
I'd rather ensure the Listview's as expected rather than coding in workarounds for data binding so it would be good if I could get a solution, essentially I only want the user to swipe right on a listitem and not allow swipe left.
Thanks again.,
Norman.

Hi,
The Renderer also affects Collection.Delete()
System.Collections.Generic.KeyNotFoundExceptionThe given key was not present
in
the dictionary.
Raw
System.ThrowHelper.ThrowKeyNotFoundException()throwhelper.cs:70
System.Collections.Generic.Dictionary<TKey, TValue>.get_Item(TKey key)
at Telerik.XamarinForms.Common.XamarinToNativeControlExtensions.UpdateCollection[T,K] (Telerik.XamarinForms.Common.T nativeElement, Telerik.XamarinForms.Common.K xfЕlement, System.String propertyName, System.Collections.Specialized.NotifyCollectionChangedEventArgs args) [0x00024]
in
<filename unknown>:0
Telerik.XamarinForms.DataControlsRenderer.iOS.ListViewRenderer.ItemsSourceCollectionChanged(
object
sender, NotifyCollectionChangedEventArgs e)
Telerik.XamarinForms.DataControls.RadListView.RaiseItemsSourceCollectionChanged(
object
sender, NotifyCollectionChangedEventArgs e)
System.Collections.ObjectModel.ObservableCollection<T>.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
System.Collections.ObjectModel.ObservableCollection<T>.OnCollectionChanged(NotifyCollectionChangedAction action,
object
item,
int
index)
System.Collections.ObjectModel.ObservableCollection<T>.RemoveItem(
int
index)
System.Collections.ObjectModel.Collection<T>.Remove(T item)
MyApp.Data.ViewModel.OrdersViewModel.RemoveOrder(
string
orderId)

I figured out the CustomRenderer for iOS to disable swipe left
public
class
CustomRadListRenderer : ListViewRenderer
{
protected
override
void
OnElementChanged(Xamarin.Forms.Platform.iOS.ElementChangedEventArgs<Telerik.XamarinForms.DataControls.RadListView> e)
{
base
.OnElementChanged(e);
Control.CellSwipeLimits =
new
UIEdgeInsets(0, 60, 0, 0);
}
}

Hi rosy,
I have opened a ticket for this and attached a repo project to the ticket.
Thanks,
Norman.