English 中文(简体)
我怎么能发现世界免疫联盟中的无休止之处?
原标题:How can I find UIElements in a rectangle in WPF?

我需要找到<条码>在(rectangle/area/spanishs)中的“钥匙Element。

Main Window 我做以下工作:

  • I register the mouse down as the start position.
  • I regsiter the mouse up position.
  • Now I need to find ll (buttons, textboxes, etc) in the rectangle between start postion and the end position.

I found in the msdn the HitTest approach but it is only for one point. I think, walking through all points in the founded rectangle it is a performance disaster.

我的守则以多国机器设备模式为基础:

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;
  }
}

任何其他想法或更好的做法?

成就

最佳回答

页: 1 (见UIElement, 而不是UIElement , 以便使用ActualWidthActualHal.

然后形成一个固定的类别,其中某些数字为<代码>。 MouseUtils

with those static fields

    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;

以及 those static methods

    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;
    }

http://www.un.org/Depts/DGACM/index_spanish.htm

Then loop over ObservableCollection<FrameworkElement> wells 以及 call that function IsElementUnderRectCursor.

This is inspired from: Kinecting the Dots

问题回答

再次感谢您的回答。 这样做了。 我只是将甄选守则从示范意见移到来。 甄选工作只在调查组进行。

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);
        }
      }
    }

usefull link for u: http://www.codeproject.com/Articles/354853/WPF-Organization-Chart-Hierarchy-MVVM-Application

当用户点击树中的 no子时,我们需要让Gis Model node知道选择已经改变。 我们愿把这次活动作为指挥,前往Gis Model





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签