@@ -406,6 +406,18 @@ void ClearContainerEventHandlers()
406406 _container . ManipulationCompleted -= OnManipulationCompleted ;
407407 _container . PointerCanceled -= OnPointerCanceled ;
408408 }
409+
410+ if ( Element is View && ElementGestureRecognizers is { } gestureRecognizers )
411+ {
412+ if ( gestureRecognizers . FirstGestureOrDefault < DragGestureRecognizer > ( ) is { } dragGesture )
413+ {
414+ dragGesture . PropertyChanged -= HandleDragAndDropGesturePropertyChanged ;
415+ }
416+ if ( gestureRecognizers . FirstGestureOrDefault < DropGestureRecognizer > ( ) is { } dropGesture )
417+ {
418+ dropGesture . PropertyChanged -= HandleDragAndDropGesturePropertyChanged ;
419+ }
420+ }
409421 }
410422 }
411423
@@ -822,26 +834,36 @@ void UpdateDragAndDropGestureRecognizers()
822834 return ;
823835 }
824836
825- bool canDrag = gestures . FirstGestureOrDefault < DragGestureRecognizer > ( ) ? . CanDrag ?? false ;
826- bool allowDrop = gestures . FirstGestureOrDefault < DropGestureRecognizer > ( ) ? . AllowDrop ?? false ;
837+ DragGestureRecognizer ? dragGesture = gestures . FirstGestureOrDefault < DragGestureRecognizer > ( ) ;
838+ DropGestureRecognizer ? dropGesture = gestures . FirstGestureOrDefault < DropGestureRecognizer > ( ) ;
827839
828- if ( canDrag )
840+ if ( dragGesture is not null )
829841 {
830- _subscriptionFlags |= SubscriptionFlags . ContainerDragEventsSubscribed ;
842+ dragGesture . PropertyChanged -= HandleDragAndDropGesturePropertyChanged ;
843+ dragGesture . PropertyChanged += HandleDragAndDropGesturePropertyChanged ;
831844
832- _container . CanDrag = true ;
833- _container . DragStarting += HandleDragStarting ;
834- _container . DropCompleted += HandleDropCompleted ;
845+ if ( dragGesture . CanDrag && ( ( _subscriptionFlags & SubscriptionFlags . ContainerDragEventsSubscribed ) == 0 ) )
846+ {
847+ _subscriptionFlags |= SubscriptionFlags . ContainerDragEventsSubscribed ;
848+ _container . CanDrag = true ;
849+ _container . DragStarting += HandleDragStarting ;
850+ _container . DropCompleted += HandleDropCompleted ;
851+ }
835852 }
836853
837- if ( allowDrop )
854+ if ( dropGesture is not null )
838855 {
839- _subscriptionFlags |= SubscriptionFlags . ContainerDropEventsSubscribed ;
856+ dropGesture . PropertyChanged -= HandleDragAndDropGesturePropertyChanged ;
857+ dropGesture . PropertyChanged += HandleDragAndDropGesturePropertyChanged ;
840858
841- _container . AllowDrop = true ;
842- _container . DragOver += HandleDragOver ;
843- _container . Drop += HandleDrop ;
844- _container . DragLeave += HandleDragLeave ;
859+ if ( dropGesture . AllowDrop && ( ( _subscriptionFlags & SubscriptionFlags . ContainerDropEventsSubscribed ) == 0 ) )
860+ {
861+ _subscriptionFlags |= SubscriptionFlags . ContainerDropEventsSubscribed ;
862+ _container . AllowDrop = true ;
863+ _container . DragOver += HandleDragOver ;
864+ _container . Drop += HandleDrop ;
865+ _container . DragLeave += HandleDragLeave ;
866+ }
845867 }
846868 }
847869
@@ -983,6 +1005,15 @@ void HandleDoubleTapped(object sender, DoubleTappedRoutedEventArgs doubleTappedR
9831005 doubleTappedRoutedEventArgs . Handled = true ;
9841006 }
9851007
1008+ void HandleDragAndDropGesturePropertyChanged ( object ? sender , System . ComponentModel . PropertyChangedEventArgs e )
1009+ {
1010+ if ( e . PropertyName == DragGestureRecognizer . CanDragProperty . PropertyName ||
1011+ e . PropertyName == DropGestureRecognizer . AllowDropProperty . PropertyName )
1012+ {
1013+ UpdateDragAndDropGestureRecognizers ( ) ;
1014+ }
1015+ }
1016+
9861017 DragEventArgs ToDragEventArgs ( UI . Xaml . DragEventArgs e , PlatformDragEventArgs platformArgs )
9871018 {
9881019 // The package should never be null here since the UI.Xaml.DragEventArgs have already been initialized
0 commit comments