Avalonia.Controls.TreeDataGrid icon indicating copy to clipboard operation
Avalonia.Controls.TreeDataGrid copied to clipboard

Collection Move not supported

Open maxkatz6 opened this issue 3 years ago • 2 comments

Randomly got this exception. Flat source was used. Shouldn't Move operation be supported as well?

Unhandled exception. System.NotSupportedException: Collection Move not supported.
   at Avalonia.Controls.Selection.TreeSelectionNode`1.OnSourceCollectionChanged(NotifyCollectionChangedEventArgs e) in /_/src/Avalonia.Controls.TreeDataGrid/Selection/TreeSelectionNode.cs:line 158
   at Avalonia.Controls.Selection.SelectionNodeBase`1.Avalonia.Controls.Utils.ICollectionChangedListener.Changed(INotifyCollectionChanged sender, NotifyCollectionChangedEventArgs e) in /_/src/Avalonia.Controls.TreeDataGrid/Selection/SelectionNodeBase.cs:line 86
   at Avalonia.Controls.Utils.CollectionChangedEventManager.<Avalonia.Utilities.IWeakSubscriber<System.Collections.Specialized.NotifyCollectionChangedEventArgs>.OnEvent>g__Notify|7_0(INotifyCollectionChanged incc, NotifyCollectionChangedEventArgs args, List`1 listeners) in /_/src/Avalonia.Controls.TreeDataGrid/Utils/CollectionChangedEventManager.cs:line 100
   at Avalonia.Controls.Utils.CollectionChangedEventManager.Avalonia.Utilities.IWeakSubscriber<System.Collections.Specialized.NotifyCollectionChangedEventArgs>.OnEvent(Object sender, NotifyCollectionChangedEventArgs e) in /_/src/Avalonia.Controls.TreeDataGrid/Utils/CollectionChangedEventManager.cs:line 117
   at Avalonia.Utilities.WeakSubscriptionManager.Subscription`1.OnEvent(Object sender, T eventArgs) in /_/src/Avalonia.Base/Utilities/WeakSubscriptionManager.cs:line 184
   at System.Collections.ObjectModel.ReadOnlyObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs args)
   at System.Collections.ObjectModel.ReadOnlyObservableCollection`1.HandleCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e)
   at System.Collections.ObjectModel.ObservableCollection`1.MoveItem(Int32 oldIndex, Int32 newIndex)
   at System.Collections.ObjectModel.ObservableCollection`1.Move(Int32 oldIndex, Int32 newIndex)
   at DynamicData.Binding.ObservableCollectionExtended`1.DynamicData.Binding.IObservableCollection<T>.Move(Int32 oldIndex, Int32 newIndex)
   at DynamicData.Binding.SortedObservableCollectionAdaptor`2.DoUpdate(ISortedChangeSet`2 updates, IObservableCollection`1 list) in /_/src/DynamicData/Binding/SortedObservableCollectionAdaptor.cs:line 93
   at DynamicData.Binding.SortedObservableCollectionAdaptor`2.Adapt(ISortedChangeSet`2 changes, IObservableCollection`1 collection) in /_/src/DynamicData/Binding/SortedObservableCollectionAdaptor.cs:line 71
   at DynamicData.ObservableCacheEx.<>c__DisplayClass27_0`2.<Bind>b__1(ISortedChangeSet`2 changes) in /_/src/DynamicData/Cache/ObservableCacheEx.cs:line 691
   at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:line 48
--- End of stack trace from previous location ---
   at System.Reactive.PlatformServices.ExceptionServicesImpl.Rethrow(Exception exception) in /_/Rx.NET/Source/src/System.Reactive/Internal/ExceptionServicesImpl.cs:line 19
   at System.Reactive.ExceptionHelpers.Throw(Exception exception) in /_/Rx.NET/Source/src/System.Reactive/Internal/ExceptionServices.cs:line 16
   at System.Reactive.Stubs.<>c.<.cctor>b__2_1(Exception ex) in /_/Rx.NET/Source/src/System.Reactive/Internal/Stubs.cs:line 16
   at System.Reactive.AnonymousObserver`1.OnErrorCore(Exception error) in /_/Rx.NET/Source/src/System.Reactive/AnonymousObserver.cs:line 73
   at System.Reactive.ObserverBase`1.OnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/ObserverBase.cs:line 59
   at System.Reactive.AutoDetachObserver`1.OnErrorCore(Exception exception) in /_/Rx.NET/Source/src/System.Reactive/Internal/AutoDetachObserver.cs:line 77
   at System.Reactive.ObserverBase`1.OnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/ObserverBase.cs:line 59
   at System.Reactive.Sink`1.ForwardOnError(Exception error) in /_/Rx.NET/Source/src/System.Reactive/Internal/Sink.cs:line 60
   at System.Reactive.Linq.ObservableImpl.Select`2.Selector._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Linq/Observable/Select.cs:line 48
   at System.Reactive.Concurrency.Synchronize`1._.OnNext(TSource value) in /_/Rx.NET/Source/src/System.Reactive/Concurrency/Synchronization.Synchronize.cs:line 44
   at System.Reactive.ObserveOnObserverNew`1.DrainStep(ConcurrentQueue`1 q) in /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:line 559
   at System.Reactive.ObserveOnObserverNew`1.DrainShortRunning(IScheduler recursiveScheduler) in /_/Rx.NET/Source/src/System.Reactive/Internal/ScheduledObserver.cs:line 513
   at Avalonia.Threading.AvaloniaScheduler.<>c__DisplayClass4_1`1.<Schedule>b__1() in /_/src/Avalonia.Base/Threading/AvaloniaScheduler.cs:line 45
   at Avalonia.Threading.JobRunner.RunJobs(Nullable`1 priority) in /_/src/Avalonia.Base/Threading/JobRunner.cs:line 37
   at Avalonia.Native.PlatformThreadingInterface.SignaledCallback.Signaled(Int32 priority, Int32 priorityContainsMeaningfulValue) in /_/src/Avalonia.Native/PlatformThreadingInterface.cs:line 39
   at Avalonia.Native.Interop.Impl.__MicroComIAvnSignaledCallbackVTable.Signaled(IntPtr this, Int32 priority, Int32 priorityContainsMeaningfulValue) in /_/src/Avalonia.Native/Interop.Generated.cs:line 3796
--- End of stack trace from previous location ---
   at Avalonia.Native.PlatformThreadingInterface.RunLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Native/PlatformThreadingInterface.cs:line 90
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken) in /_/src/Avalonia.Base/Threading/Dispatcher.cs:line 65
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 120
   at Avalonia.ClassicDesktopStyleApplicationLifetimeExtensions.StartWithClassicDesktopLifetime[T](T builder, String[] args, ShutdownMode shutdownMode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 209
   at Program.Main(String[] args)

maxkatz6 avatar Apr 20 '22 12:04 maxkatz6

I have made two extension methods Sort and AddItem for ObservableCollection and in it I use Move(old index, newindex). And I get the same exception in TreeDataGrid, but not always. After the Move() method works, notification occurs in the TreeDataGrid, as I understand it. And something is going wrong(:

public static class ObservableCollectionExt
{
    public static void Sort<T>(this ObservableCollection<T> collection) where T : IComparable
    {
        List<T> sorted = collection.OrderBy(x => x).ToList();
        
        for (int i = 0; i < sorted.Count(); i++)
        {
            collection.Move(collection.IndexOf(sorted[i]), i);
        }
    }

    public static void AddItem<T>(this ObservableCollection<T> collection, T item) where T : IComparable
    {
        collection.Add(item);
        
        var oldIndex = collection.IndexOf(item);
        
        List<T> sorted = collection.OrderBy(x => x).ToList();

        var newIndex = sorted.IndexOf(item);

        collection.Move(oldIndex, newIndex);
    }
}

MaximGubanov avatar Jun 06 '24 15:06 MaximGubanov

And something is going wrong

It's just as simple. Move operation is not supported in this control. You need to use Remove and Insert instead.

maxkatz6 avatar Jun 06 '24 15:06 maxkatz6