【问题标题】:Restricting usercontrol from moving beyond the canvas xaml限制用户控件超出画布 xaml
【发布时间】:2014-02-20 12:44:48
【问题描述】:

我正在使用 XAML 开发 Windows 8.1 应用商店应用程序。

场景是我有一个画布,我在其中放置了多个用户控件。单击按钮可以移动用户控件。我应该限制用户控件移动到画布之外。有什么办法吗。?我在用户控件中有用于移动的操作事件。

【问题讨论】:

    标签: c# .net windows xaml windows-store-apps


    【解决方案1】:

    我希望您移动的控件是画布上的子控件? 如果是这样,我认为您可以检查一下您控制在画布上移动的边界是否触及画布的边界。如果发生这种情况,请停止。

    基本游戏编程的东西。我会找一个例子

    【讨论】:

    • 是的。控件是画布中的子控件。用户控件具有 ManipulationStarted、ManipulationDelta 和 ManipulationCompleted 等事件来处理移动。问题是我不知道如何在移动时检查画布的边界。
    【解决方案2】:

    在这里。

    这部分代码将为您提供容器内的控件位置

    public static class BoundsHelper
    {
        public static Rect GetBoundsInParentContainer(Control control)
        {
            Vector offset = VisualTreeHelper.GetOffset(control);
            Rect rect = new Rect(offset.X, offset.Y, control.ActualHeight, control.ActualWidth);
            return rect;
        }
    }
    

    因此,如果您在画布的左上角有一个按钮,并且您向该按钮询问控件所在的位置,它将为您提供 X 和 Y 为 0 以及控件的宽度和高度的矩形。因此,如果您将控件移动到画布内。调用此方法并检查您的控件是否仍在画布的高度和宽度内。

    这里是向右移动的代码,但首先检查你是否被允许移动

    private void MoveRightAndCheck(Rect boundsControl, Control container)
        {
            int stepSize = 10;
    
            if (container.ActualWidth > (boundsControl.X + boundsControl.Width + stepSize))
            {
                //Your code to move your control inside the canvas
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-19
      • 2012-12-22
      • 1970-01-01
      相关资源
      最近更新 更多