WPF在列表框中实现橡皮带类型选择

时间:2009-09-03 14:11:10

标签: listbox selection hittest rubber-band lasso

我正在尝试允许橡皮筋或套索类型选择用户想要选择的列表框中的项目。我的列表框位于网格和网格中,我添加了一个控件,在我想要选择的区域上绘制一个矩形。我尝试过测试列表框项目以查看它们是否属于矩形但是它们似乎都返回它们没有。当查看这些项目的VisualTreeHelper.GetDescendantBounds时(就像我为矩形获取它的X,Y),它总是返回X,Y为每个项目的0,0。我在测试中做错了什么?

1 个答案:

答案 0 :(得分:0)

您可以使用此代码获取UIElements相对于另一个UIElement的位置和界限。代码取自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;
        }
    }
}