在WPF中制作可移动控件
我有一个面板,该面板内有几个矩形控件(控件的数量不同),我希望用户能够在面板内移动控件,以便他们可以以最适合他们的方式排列控件。有没有人有任何我可以阅读的资源或简单的提示,可以帮助我走上正确的道路?
谢谢
I have a panel, within that panel are several rectangular controls (the number of controls vaires) I want the user to be able to move the controls around within the panel so that they can arrange the controls in the way that suits them best. does anyone have any resources i could read or simple tips which would get me headed down the right road?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想出了一种以拖动/移动方式移动控件的可能的简单方法...以下是步骤。
在第一个 MouseDown 事件上,使用以下代码设置一些 Point 属性 (InitialPosition)
现在您已经有了起始位置,您需要获取鼠标相对于移动控件的位置。这样您就不必最终单击标题的中间来移动它,它会立即将控件的左上角移动到鼠标指针位置。为此,请将此代码放置在 MouseDown 事件中:
现在您已经有了控件的起始点(startX 和 STartY),即鼠标在移动控件中的位置(RelativeX、RelativeY),我们只需移动控制到一个新的位置!执行此操作需要执行几个步骤。首先,您的控件需要有一个 RenderTransform,它是一个 TranslateTransform。如果您不想在 XAML 中设置它,请随意使用
this.RenderTransform = new TranslateTransform
进行设置。现在我们需要在 RenderTransform 上设置 X 和 Y 坐标,以便控件移动到新位置。以下代码完成此任务
正如你可以猜到的,有一些代码被遗漏了(变量声明等),但这应该是你开始所需的全部:)快乐的编码。
编辑:
您可能遇到的一个问题是,这允许您将控件移出其父控件的区域。下面是一些快速但肮脏的代码来解决该问题...
在实际移动发生之前将此代码放入 MouseMove 事件中。这将检查控件是否试图移出父控件的边界。
IsMoving = false
命令将导致控件退出移动模式。这意味着用户需要再次单击移动区域才能尝试移动控件,因为控件将停在边界处。如果您希望控件自动继续移动,只需将该行取出,一旦光标回到合法区域,控件就会跳回到光标上。I figured out a possible, simple method of moving a control in a drag/move style... Here are the steps.
On the first MouseDown event, set some Point property (InitialPosition) using the following code
Now that you have the starting position, you need to get the position of the mouse relative to your movement control. This is so you dont end up clicking the middle of your header to move it and it instantly moves the top left of the control to the mouse pointer location. To do this, place this code in the MouseDown event:
Now you have the point the control originated at (startX and STartY), the position of the mouse within your movement control (RelativeX, RelativeY), we just need to move the control to a new location! There are a few steps involved in doing this. Firstly your control needs to have a RenderTransform which is a TranslateTransform. If you dont want to set this in XAML, feel free to set it using
this.RenderTransform = new TranslateTransform
.Now we need to set the X and Y coordinates on the RenderTransform so that the control will move to a new location. The following code accomplishes this
As you can guess, there is a bit of code left off(variable declarations etc) but this should be all you need to get you started :) happy coding.
EDIT:
One problem you may encounter is that this allows you to move the control out of the area of its parent control. Here is some quick and dirty code to fix that issue...
Place this code in your MouseMove event before the actual movement takes place. This will check if the control is trying to move outside the bounds of the parent control. The
IsMoving = false
command will cause the control to exit movement mode. This means that the user will need to click the movement area again to try to move the control as it will have stopped at the boundary. If you want the control to automatically continue movement, just take that line out and the control will jump back onto the cursor as soon as it is back in a legal area.您可以在这里找到很多灵感:
http://www.codeproject.com/KB /WPF/WPFDiagramDesigner_Part1.aspx
http://www.codeproject.com/ KB/WPF/WPFDiagramDesigner_Part2.aspx
http://www.codeproject.com /KB/WPF/WPFDiagramDesigner_Part3.aspx
http://www.codeproject. com/KB/WPF/WPFDiagramDesigner_Part4.aspx
You can find a lot of inspiration here:
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part1.aspx
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part2.aspx
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part3.aspx
http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part4.aspx