views:

405

answers:

1

I'm trying to allow rubberband or lasso type selection of the items in the listbox that the user wants selected. My Listbox is in a grid and to the grid I added a control that draws a rectangle over the area I want to select. I've tried hit testing the listbox items to see if they fall within the rectangle but they all seem to return that they don't. When looking at the VisualTreeHelper.GetDescendantBounds for those items (like I do for the rectangle to get it's X,Y) it always returns X,Y as 0,0 for each of the items. What am I doing wrong with the hittesting?

A: 

You could use this code to get the position and bounds of UIElements relative to another UIElement. The code is taken from this post.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

public static class UIHelper
{
    public static Boolean GetUIElementCornersRelativTo(UIElement Base,
                                                UIElement RelativeTo,
                                                ref Point TopLeft,
                                                ref Point BottomLeft,
                                                ref Point BottomRight,
                                                ref Point TopRight,
                                                ref String Message)
    {
        try
        {
            if (Base == null)
            {
                throw new Exception("Base UIElement is null");
            }
            if (RelativeTo == null)
            {
                throw new Exception("RelativTo UIElement is null");
            }

            TopLeft = Base.TranslatePoint(new Point(0, 0), RelativeTo);
            BottomLeft = Base.TranslatePoint(new Point(0, Base.RenderSize.Height), RelativeTo);
            BottomRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, Base.RenderSize.Height), RelativeTo);
            TopRight = Base.TranslatePoint(new Point(Base.RenderSize.Width, 0), RelativeTo);

            Message = "OK";
            return true;
        }
        catch (Exception ex)
        {
            Message = ex.Message;
            return false;
        }
    }
    public static Boolean GetPointRelativTo(UIElement Base,
                                     UIElement RelativeTo,
                                     Point ToProjectPoint,
                                     ref Point Result,
                                     ref String Message)
    {
        try
        {
            if (Base == null)
            {
                throw new Exception("Base UIElement is null");
            }
            if (RelativeTo == null)
            {
                throw new Exception("RelativTo UIElement is null");
            }

            if (ToProjectPoint == null)
            {
                throw new Exception("To project point is null");
            }

            Result = Base.TranslatePoint(ToProjectPoint, RelativeTo);

            Message = "OK";
            return true;
        }
        catch (Exception ex)
        {
            Message = ex.Message;
            return false;
        }
    }
}
bitbonk