C#和WPF-如何将拖动的对象放置到固定位置(网格). [英] C# and WPF - how to drop dragged object to the fixed position (grid).

查看:751
本文介绍了C#和WPF-如何将拖动的对象放置到固定位置(网格).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我需要一点帮助,所以我希望能在这里找到答案.
我正在做学校考试-使用WPF的C#棋盘游戏.有代表游戏作品的椭圆.拖动的同时,我使用了Josh Smith的类"DragCanvas",在此 http://www.codeproject.com/KB/WPF/DraggingElementsInCanvas.aspx 中进行了描述.
我的问题是如何将省略号降到游戏板上的确切位置.我使用网格来使字段居中,现在我打算采用impement drop方法,该方法将拖动的椭圆居中到释放了mouseleftbutton的网格字段上.同时,我希望只允许在指定的网格位置内放置碎片,而不能在其他位置放置.
我真的很感激任何想法,因为时间是目前对我来说最大的敌人.
多亏了所有的人.
Lukas.

解决方案

如果要将项目在网格中的位置设置为特定坐标(例如200、422),而不是逻辑位置(例如(居中),您可以设置控件的边距.要允许放置仅在特定区域内发生,您可以让代码检查在拖动过程中放置​​在那些区域内(并且可以相应地更改拖动图标以指示是否允许放置),或者可以覆盖网格位于主网格之上,并在其上启用了拖放功能,而不是在主网格上启用了拖放功能(执行拖放操作后,控件将被添加到主控件而不是覆盖网格中).

谢谢,这听起来合乎逻辑.但是还有与此有关的问题.实际上,"DragCanvas"库是使用"xmlns:local =" clr-namespace:MyNamespace"应用于整个窗口的,因此窗口的所有对象都是可拖动的.我可以使用"local:DragCanvas.CanBeDragged =" False"拒绝某些对象的拖动,但是如果我对任何对象使用属性"AllowDrop =" False",则它不起作用(仍然可以将椭圆删除)我不知道为什么.
我是新手,所以我会感谢任何有助于我前进的简单代码.


Hello,
i need a small help so I hope will find answer here.
I''m making school exam - a board game in C# using WPF. There are ellipses representing playing pieces. Meantime for dragging I used class "DragCanvas" from Josh Smith described here http://www.codeproject.com/KB/WPF/DraggingElementsInCanvas.aspx.
What is my problem is HOW TO DROP ELLIPSE TO THE EXACT POSITION ON THE GAME BOARD. I used grid to center the fields and now I plan to impement drop method which center dragged ellipse to the grid field, where mouseleftbutton was released. Simultaneously I want to allow pieces drop only inside specified grid positions and not elsewhere.
I''ll really appreciate any idea what way to go because time is the worst enemy for me at the moment.
Thanks to all in advance.
Lukas.

解决方案

If you want to set the position of an item within a grid to a particular coordinate (e.g., 200, 422) rather than a logical position (e.g., center), you can set the margin of the control. To allow the drop to only occur within certain regions, you can have your code check that the drop is within those regions during the drag (and you can change the drag icon accordingly to indicate whether or not a drop is allowed), or you can overlay grids on top of your primary grid and have the drag/drop functionality enabled on them rather than the primary grid (when a drop is performed, the control would then be added to the primary control rather than the overlay grids).


Thanks, that sounds logically. But there are questions related to it. Actually the "DragCanvas" library is applied to the whole window using "xmlns:local="clr-namespace:MyNamespace"" so all the objects of the window are draggable. I can deny dragging of some objects using "local:DragCanvas.CanBeDragged="False"" but If I use property "AllowDrop="False"" for any object it doesn''t work (ellipse can be still dropped to it) and I don''t know why.
I''m newbie in that so I would appreciate any saple code which helps me move.


这篇关于C#和WPF-如何将拖动的对象放置到固定位置(网格).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆