punker76 / gong-wpf-dragdrop

The GongSolutions.WPF.DragDrop library is a drag'n'drop framework for WPF
BSD 3-Clause "New" or "Revised" License
2.25k stars 392 forks source link

Try to catch an InvalidOperationException? #469

Closed PatrickKursawe closed 9 months ago

PatrickKursawe commented 1 year ago

I know this is a poor report, but it is all information I have got - just a stacktrace.

InvalidOperationException: This Visual is not connected to a PresentationSource. [GongSolutions.WPF.DragDrop]GongSolutions.Wpf.DragDrop.DragDrop/<>c internal void b34_0(Point point); IL Offset=0x0013. [GongSolutions.WPF.DragDrop]GongSolutions.Wpf.DragDrop.Utilities.MouseHelper/<>c__DisplayClass1_0 internal void b0(object , EventArgs ); IL Offset=0x0028. [...] skipping parts in Windows [GongSolutions.WPF.DragDrop]GongSolutions.Wpf.DragDrop.DragDrop private static void DoDragSourceMove(object sender, Func`2<class [PresentationCore]System.Windows.IInputElement,valuetype [WindowsBase]System.Windows.Point> getPosition); IL Offset=0x01b8. [GongSolutions.WPF.DragDrop]GongSolutions.Wpf.DragDrop.DragDrop private static void DragSourceOnMouseMove(object sender, MouseEventArgs e); IL Offset=0x007c.

System is Windows 10.0.19045.0 The drag/drop operation is on a DataGrid inside an element host of a windows forms app. <DataGrid CanUserSortColumns="False" ItemsSource="{Binding Parameters}" Margin="0,0,0,0" x:Name="GrdParams" AutoGenerateColumns="False" SelectionUnit="FullRow" SelectionMode="Extended" gong:DragDrop.IsDragSource="True" gong:DragDrop.IsDropTarget="True" gong:DragDrop.UseDefaultDragAdorner="True" gong:DragDrop.UseVisualSourceItemSizeForDragAdorner="True" CanUserAddRows="{Binding CanAddRemove}" CanUserDeleteRows="{Binding CanAddRemove}" >

As this error already happened a few times, but I really can't find a way to reproduce it at will, I would suggest that you maybe just catch this kind of exception in DoDragSource move and ignore the attempted operation?

I am using version 3.2.1 or your package. Thanks!

punker76 commented 9 months ago

@PatrickKursawe You can handle exceptions by yourself. You need to derive (as an example) from the DefaultDragHandler or create a complete new one from IDragSource and use then that at your ItemsSource.

    public class DefaultDragHandlerImpl : DefaultDragHandler
    {
        public override bool TryCatchOccurredException(Exception exception)
        {
            if (exception is InvalidOperationException)
            {
                // log or do something special

                // with this you say that you handle this exception
                return true;
            }

            return base.TryCatchOccurredException(exception);
        }
    }
PatrickKursawe commented 9 months ago

Thanks for the hint, will do.