views:

81

answers:

2

I'm writing a user control in WPF which is based on a ListBox. One of the main pieces of functionality is the ability to reorder the list by dragging the items around. When a user drags an item I change the items Opacity to 50% and physically move the item in an ObservableCollection in my ViewModel depending on where the user wants it. On the drop event I change the Opacity back to 100%.

The problem I've got is that if the user drags the item off my control and drops it somewhere else then I need to change the Opacity back to 100% and move the item back to where it was when the user started the drag. Is there an event I can handle to capture this action? If not is there any other cunning way to solve this problem?

+1  A: 

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

This of course assumes that the other controls on your form do not allow the dropping of your list items.

itowlson
+1  A: 

I've done this using event triggers in XAML before. In my case, they were transparent (30% opacity) before and when the user drags over, the opacity is set to 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>

The storyboards would then animate the opacity

<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>
Isak Savo