如何在WPF中的矩形中找到UIElements?

时间:2012-04-27 14:30:55

标签: wpf mvvm rectangles uielement

我需要在(矩形/区域/边界)中找到UIElement

MainWindow我正在做以下事情:

  • 我将鼠标注册为起始位置。
  • 我注定了鼠标位置。
  • 现在我需要在start之间的矩形中找到ll(按钮,文本框等)  位置和结束位置。

我在msdn中找到了HitTest方法,但它只有一点。我想,走过创始人的所有观点 矩形,这是一场性能灾难。

http://msdn.microsoft.com/en-us/library/ms752097.aspx

我的代码基于MVVM模式:

private ObservableCollection<UIElementViewModel> wells;   
private Point stratPoint; // Mouse down
public ICommand MouseUpRightCommand
{
  get
  {
    if (this.mouseUpRightCommand == null)
    {
      this.mouseUpRightCommand = new RelayCommands(
        param =>
       {
          if (param is MouseButtonEventArgs)
          {
            var e = (param as MouseButtonEventArgs);

            //Set the end point
            endPosition = e.GetPosition(((ItemsControl)e.Source));

            // for example, here I want to find all controls(UIElements) in the
            // founded rectangle of stratPoint and endPosition.

          }
        });
    }

    return this.mouseUpRightCommand;
  }
}

还有其他想法或更好的方法吗?

由于

3 个答案:

答案 0 :(得分:3)

我会使用FrameworkElement(扩展UIElement)代替UIElement,以便使用ActualWidthActualHeight属性

然后创建一个静态类,它可以进行一些数学MouseUtils

使用那些静态字段

    private static double _dContainerTop;
    private static double _dContainerBottom;
    private static double _dContainerLeft;
    private static double _dContainerRight;
    private static double _dCursorTop;
    private static double _dCursorLeft;
    private static double _dCursorRight;
    private static double _dCursorBottom;

和那些静态方法

    private static void FindValues(FrameworkElement element, Visual rootVisual)
    {
        var containerTopLeft = container.TransformToAncestor(rootVisual).Transform(new Point(0, 0));

        _dContainerTop = containerTopLeft.Y;
        _dContainerBottom = _dContainerTop + container.ActualHeight;
        _dContainerLeft = containerTopLeft.X;
        _dContainerRight = _dContainerLeft + container.ActualWidth;

    }

    public static bool IsElementUnderRectCursor(FrameworkElement element, Point startPoint, Point endPoint, Visual rootVisual)
    {
       _dCursorTop=Math.Min(startPoint.Y, endPoint.Y);
       _dCursorBottom=Math.Max(startPoint.Y, endPoint.Y);
       _dCursorLeft=Math.Min(startPoint.X, endPoint.X);
       _dCursorRight=Math.Max(startPoint.X, endPoint.X);

        FindValues(container, rootVisual);
        if (_dContainerTop < _dCursorTop|| _dCursorBottom< _dContainerBottom )
        {
            return false;
        }
        if (_dContainerLeft < _dCursorLeft|| _dContainerRight < _dCursorRight)
        {
            return false;
        }
        return true;
    }
例如,

Rootvisual是你的窗口;

然后循环ObservableCollection<FrameworkElement> wells并调用该函数IsElementUnderRectCursor

这是受到以下启发: Kinecting the Dots

答案 1 :(得分:0)

Astreal再次感谢您的回答。完成。我刚刚将选择代码从modelView移到了视图中。选择仅在UI中完成。

private void SelectWells(RectangleGeometry selectionRectangle, FrameworkElement frameworkElement)
    {
      var items = GetItemsControl(frameworkElement);

      foreach (var item in items.Items)
      {
        var viusalItem = (ContentPresenter)items.ItemContainerGenerator.ContainerFromItem(item);

        var wellControl = this.GetWellControl(viusalItem);

        var relativePoint = wellControl.TransformToAncestor(items).Transform(new Point(0, 0));

        var controlRectangle =
          new RectangleGeometry(
            new Rect(relativePoint.X, relativePoint.Y, wellControl.ActualWidth, wellControl.ActualHeight));

        var intersectionGeometry = Geometry.Combine(
          selectionRectangle, controlRectangle, GeometryCombineMode.Intersect, null);

        if (intersectionGeometry.GetArea() > 0)
        {
          wellControl.Command.Execute(this);
        }
      }
    }

答案 2 :(得分:-1)

你的有用链接: http://www.codeproject.com/Articles/354853/WPF-Organization-Chart-Hierarchy-MVVM-Application

当用户点击树中的节点时,我们需要让ViewModel节点知道选择已更改。我们喜欢将事件作为命令路由到ViewModel