Silverlight MouseDragElementBehavior. How to change positions after drag and drop?

I have a stack panel with custom controls. User can add or remove items.

I bound MouseDragElementBehavior to each element. Now the user can move them inside the stack panel.

However, the elements are now randomly ordered. Really a mess. They remain where the user left them.

Now I need to make them complex, since the stack panel should be ... Beautiful one after another ...

Therefore, I just need to let the user change the order of the elements using the drag and drop operation, but the elements must be stacked exactly.

There is a DragFinished event, but I really don't see how the behavior moves the elements. I thought it was Margin, but the changes remain 0 ... I do not know what to do next.

Appreciate a little help.

+4
source share
1 answer

MouseDragElementBehavior does its job using the Transform in the RenderTransform property of the attached element (that is, the one to which the behavior applies) (the exact type of transformation depends on the state of the RenderTransform property, but it will either TranslateTransform or MatrixTransform).

The conversion does not reset when you finish dragging (and you don’t want it to be because the element will snap to its position in the StackPanel when you started dragging), and that is why the element remains where the user left them.

To change the position of the positions in the StackPanel as you say, you have to use the DragFinished event. What you probably want to do is work on where the element ends in the StackPanel (by dragging and dropping, that is, which element in the panel he β€œclicked”), create an animation to animate the element from its current one ( where the user released drag and drop) to this end position in the StackPanel and another animation to move the "pressed" item to its new position in the StackPanel ( VisualTreeHelper might help here (I think)). Once these animations are finished, simply set a new index in the StackPanel for each item and delete the RenderTransform translation.

+1
source

Source: https://habr.com/ru/post/1303605/


All Articles