在运行时拖动多个元素超出画布边界 [英] Dragging multiple elements at runtime going beyond the canvas boundary

查看:180
本文介绍了在运行时拖动多个元素超出画布边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个wpf应用程序,用于在餐厅安排表格(这是一个切换按钮控件)。可以选择多选并拖动所有选定的表。我需要避免任何超出画布边界的表格。
下面给出的
是鼠标移动事件中的代码。



谢谢提前..



我的尝试:



I have a wpf application which is used for arrangement of tables(which is a toggle button control) in restaurant. There is an option to multi select and drag all the selected tables. I need to avoid any of the tables going outside the canvas boundary.
below given is my code in mouse move event.

Thanks in Advance..

What I have tried:

Dim itemCtrls As IEnumerable(Of ToggleButton) = DesigningCanvas.Children.OfType(Of ToggleButton)()
Dim selbtns As List(Of ToggleButton) = (From tglBtn As ToggleButton In itemCtrls
                                       Where tglBtn.IsChecked
                                       Select tglBtn).ToList()


' Retrieving the item's current x and y position
Dim xPos As Double = e.GetPosition(Nothing).X - _pos.X
Dim yPos As Double = e.GetPosition(Nothing).Y - _pos.Y

If (selbtns.Count > 1) Then '' if selected button to drag is more than one

    For Each elem In selbtns
        elem.SetValue(Canvas.LeftProperty, xPos + CDbl(elem.GetValue(Canvas.LeftProperty)))
        elem.SetValue(Canvas.TopProperty, yPos + CDbl(elem.GetValue(Canvas.TopProperty)))
        _pos = e.GetPosition(Nothing)
    Next
Else '' for a single item drag


    TryCast(_movingObject, FrameworkElement).SetValue(Canvas.LeftProperty, _
                                                      e.GetPosition(TryCast _
                                                                    (TryCast(_movingObject, FrameworkElement).Parent, FrameworkElement)).X - _firstXPos)

    TryCast(_movingObject, FrameworkElement).SetValue(Canvas.TopProperty, e.GetPosition(TryCast(TryCast(_movingObject, FrameworkElement).Parent, FrameworkElement)).Y - _firstYPos)
    _pos = e.GetPosition(Nothing)
End If

推荐答案

引用:

我需要避免任何超出画布边界的表格。

I need to avoid any of the tables going outside the canvas boundary.

简单,你只需要检查你移动的表格是否为bumpin g画布的边缘。



[更新]

对于表格,你知道方形/矩形(Xmin,Xmax,Ymin, Ymax)适合桌子;如果选择了一组表格,则必须为该组表格计算矩形。

然后检查矩形是否在画布中。

Simple, you only have to check if the tables you move are bumping the edge of canvas.

[Update]
For a table, you know the square/rectangle (Xmin, Xmax, Ymin, Ymax) in which fit the table; When you have a set of tables selected, you must calc the rectangle for the set of tables.
Then you check if the rectangle is in canvas or not.


这篇关于在运行时拖动多个元素超出画布边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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