Silverlight MouseDragElementBehavior。拖放后如何重新排列项目?
我有带有自定义控件的堆栈面板。用户可以添加或删除项目。
我已将 MouseDragElementBehavior 附加到每个项目。所以现在用户可以在堆栈面板中移动它们。
然而,现在的项目是以任意方式排列的。实在是一团糟。它们停留在用户离开的地方。
我现在需要的是让它们按照堆栈面板应该的方式堆叠...很好地一一...
所以我需要简单地让用户通过使用拖/放操作来更改项目的顺序,但是项目必须精确堆叠。
有 DragFinished 事件,但我并没有真正看到行为如何移动项目。我以为是边距发生了变化,但边距保持为 0...我不知道下一步该怎么做。
感谢小小的帮助。
I have stack panel with custom controls in it. User can add or remove the items.
I have attached MouseDragElementBehavior to each item. So now user can move them within the stack panel.
However the items now are arranged on arbitrary manner. Is a mess really. They stay where the user left them.
What I need now is to make them to be stacked as the stack panel supposed to be... Nicely one by one...
So I need to simply let user change the order of items by using drag / drop operation but items has to be stacked precisely.
There is DragFinished event, but I dont really see how the Behavior moves items. I thought it is Margin it changes but margins stays 0... I dont know what to do next.
Appreciate little help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MouseDragElementBehavior 使用附加元素(即应用行为的元素)的 RenderTransform 属性上的 Transform 来完成其工作(变换的确切类型取决于 RenderTransform 属性的状态,但它将是 TranslateTransform 或 MatrixTransform) 。
当您完成拖动时,变换不会重置(您也不希望它重置,因为当您开始拖动时,元素会弹回其在 StackPanel 中的位置),这就是元素保留在“用户离开它们”的位置的原因。
要按照您所说的方式更改 StackPanel 中的项目位置,您必须使用 DragFinished 事件。您可能想要做的是计算出该元素在 StackPanel 中的最终位置(由于拖动,即面板中的哪个元素将“推”下),然后创建一个动画以从当前元素开始制作动画位置(用户释放拖动的位置)到 StackPanel 中的最终位置,并使用另一个动画将“推入”元素移动到 StackPanel 中的新位置 (VisualTreeHelper 可能可以在这里提供帮助(我认为))。这些动画完成后,只需在 StackPanel 中为每个项目设置新索引并删除 RenderTransform 转换即可。
MouseDragElementBehavior does its work using a Transform on the RenderTransform property of the attached element (i.e. the one the behavior is applied to) (the exact type of transform depends on the state of the RenderTransform property but it will either be a TranslateTransform or a MatrixTransform).
The transform is not reset when you finish dragging (nor would you want it to be because the element would snap back to its position in the StackPanel when you started dragging) and this is why the element stays where "the user left them".
To change the items position in the StackPanel in the way you are talking about you will have to use the DragFinished event. What you probably want to do is work out where in the StackPanel the element will end up (as a result of the drag, i.e. which element in the panel it will "push" down) then create an animation to animate the element from its current position (where the user released the drag) to that final position in the StackPanel and another animation to move the "pushed" element in to its new position in the StackPanel (VisualTreeHelper can probably help here (I think)). Once those animations are finished just set the new index within the StackPanel for each item and remove the RenderTransform translation.