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

发布于 2024-08-03 23:11:24 字数 291 浏览 5 评论 0原文

我试图允许橡皮筋或套索类型选择用户想要选择的 Listbox 中的项目。我的 Listbox 位于网格中,并且我在网格中添加了一个控件,该控件在我想要选择的区域上绘制一个矩形。我尝试过对 Listbox 项目进行点击测试,看看它们是否落在矩形内,但它们似乎都返回没有落在矩形内。当查看这些项目的 VisualTreeHelper.GetDescendantBounds 时(就像我对矩形所做的那样,以获得它的 X,Y),它总是为每个项目返回 X,Y 作为 0,0。我在命中测试中做错了什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2024-08-10 23:11:24

您可以使用此代码来获取 UIElement 相对于另一个 UIElement 的位置和边界。该代码取自这篇文章

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

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;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文