How to find out if drag and drop was canceled in WPF

I am writing a user control in WPF that is based on a ListBox. One of the main features is the ability to reorder the list by dragging items around. When a user drags an element, I change the elements Opacityby 50% and physically move the element in ObservableCollectionin my ViewModel depending on where he wants it. In the drop event, I change Opacityto 100%.

The problem is that if the user drags the item out of my control and drops it somewhere else, I need to change it Opacityback 100% and move the item back to where it was when the user started dragging. Is there some kind of event that I can handle to capture this action? If not, is there another tricky way to solve this problem?

+5
source share
2 answers

Assuming you are using the built-in drag and drop function, you can use the return value of the DoDragDrop method. If the drag target does not accept the drag object, DoDragDrop returns DragDropEffects.None.

, , , .

+13

, XAML . (30% ) , ​​ 100%.

<EventTrigger RoutedEvent="DragDrop.DragEnter">
  <BeginStoryboard Storyboard="{StaticResource FadeInStoryboard}" x:Name="FadeInStoryboard_BeginStoryboard1"/>
</EventTrigger>
<EventTrigger RoutedEvent="DragDrop.DragLeave">
  <BeginStoryboard Storyboard="{StaticResource FadeOutStoryboard}" x:Name="FadeOutStoryboard_BeginStoryboard1"/>
</EventTrigger>

<Storyboard x:Key="FadeInStoryboard">
    <DoubleAnimation To="1" Duration="0:00:00.2" Storyboard.TargetName="UserControl" Storyboard.TargetProperty="(UIElement.Opacity)" />
</Storyboard>
<Storyboard x:Key="FadeOutStoryboard">
  <DoubleAnimation To="0.3" Duration="0:00:00.2" Storyboard.TargetName="UserControl" Storyboard.TargetProperty="(UIElement.Opacity)" />
</Storyboard>
+1

All Articles