在 wpf 中使 StackPanel 及其内容“可拖动”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12320406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make a StackPanel and its contents "draggable" in wpf?
提问by user1202434
I have a UserControl in the form of:
我有一个 UserControl 的形式:
<UserControl>
<Grid>
<!-- Content Here-->
<StackPanel> <!-- I want this element to be draggable within the usercontrol. -->
<Label Name = "Header" />
<Button />
<Button />
<Button />
</StackPanel>
<Grid>
</UserControl>
My end result is to have a control with buttons (part of the usercontrol) that can be dragged around..? That is, moveable ... within the UserControl
我的最终结果是有一个可以拖动按钮的控件(用户控件的一部分)。也就是说,在 UserControl 中可移动...
Ive read about Thumb but I am not sure how to use it...Any ideas or examples would be great.Thanks!
我读过关于 Thumb 的内容,但我不知道如何使用它......任何想法或例子都会很棒。谢谢!
回答by Rachel
A very simple way to do it would be to use mouse events
一个非常简单的方法是使用鼠标事件
First off, wrap your StackPanelin a Canvasso it can be easily positioned according to where the user drags it
首先,将您的包裹StackPanel在 a 中,Canvas以便根据用户拖动它的位置轻松定位
Next, add MouseDownand MouseUpevents to the StackPanel. You may need to give your StackPanela background color for it to receive mouse events.
接下来,添加MouseDown和MouseUp事件的StackPanel。您可能需要为其提供StackPanel背景颜色以接收鼠标事件。
In the MouseDownevent, attach a MouseMoveevent handler to the StackPaneland have the panel capture the mouseso all mouse events will get handled by the StackPanel.
在该MouseDown事件中,将MouseMove事件处理程序附加到StackPanel并让面板捕获鼠标,以便所有鼠标事件都将由StackPanel.
In the MouseUpevent, detach the MouseMoveevent and release your mouse capture.
在MouseUp事件中,分离MouseMove事件并释放鼠标捕获。
In the MouseMoveevent, change the Canvas.Topand Canvas.Leftproperties of the panel based on the current Mouse position. You will need a check here to determine if the mouse is outside of the UserControltoo so the StackPanelcan't be dragged off screen.
在MouseMove事件中,根据当前鼠标位置更改面板的Canvas.Top和Canvas.Left属性。您需要在此处进行检查以确定鼠标是否UserControl也在之外,因此StackPanel无法将其拖出屏幕。
And that's it, very basic drag drop :)
就是这样,非常基本的拖放:)
回答by Ignacio Soler Garcia
One way would be to handle the mouseclick on the button and change its position while moving the mouse.
一种方法是处理鼠标点击按钮并在移动鼠标时改变其位置。
So the steps will be:
所以步骤将是:
- Add a handler to the button. Mouseclick for example (or mousedown)
- When you receive a mouseclick add a handler to mousemove.
- Check the movement of the mouse and change the position of the button
- Remove the handler after releasing the mouse button.
- 向按钮添加处理程序。例如鼠标点击(或鼠标按下)
- 当您收到 mouseclick 时,将处理程序添加到 mousemove。
- 检查鼠标的移动并改变按钮的位置
- 释放鼠标按钮后删除处理程序。
回答by Can Poyrazo?lu
I don't know of any specific method but intuitively, you may add an event handler to drag method of the control you want to move, and in that method, you may render the control to a bitmap, make that bitmap follow your mouse pointer, and hide the original control. When dropped (mouse released), it should simply set the original control's coordinates to mouse coordinates, dispose of the bitmap, and re-enable the visibility of the control.
我不知道任何具体的方法,但直觉上,您可以添加一个事件处理程序到要移动的控件的拖动方法,在该方法中,您可以将控件渲染为位图,使该位图跟随您的鼠标指针, 并隐藏原来的控件。当放下(鼠标释放)时,它应该简单地将原始控件的坐标设置为鼠标坐标,处理位图,并重新启用控件的可见性。
回答by Hasan Zubairi
There are some tutorials available for this on MSDN and other places. Try the following link for WPF drag and drop.
在 MSDN 和其他地方有一些可用的教程。尝试使用以下链接进行 WPF 拖放。
回答by Steve B
You should give a chance to the avalondock project.
你应该给avalondock 项目一个机会。
This allows you to create rich layout features, like Visual Studio does (floating, docking, etc.).
这允许您创建丰富的布局功能,就像 Visual Studio 所做的那样(浮动、停靠等)。
I'm confident this would help you.
我相信这会对你有所帮助。

