【问题标题】:WPF implementing a rubberband type selection within a listboxWPF 在列表框中实现橡皮筋类型选择
【发布时间】:2009-09-03 14:11:09
【问题描述】:

我正在尝试允许用户希望选择Listbox 中的项目的橡皮筋或套索类型选择。我的Listbox 在网格中,我在网格中添加了一个控件,该控件在我要选择的区域上绘制一个矩形。我已经尝试测试Listbox 项目以查看它们是否在矩形内,但它们似乎都返回它们没有。在查看这些项目的VisualTreeHelper.GetDescendantBounds 时(就像我对矩形所做的那样,得到它的 X,Y)它总是将每个项目的 X,Y 返回为 0,0。我在命中测试方面做错了什么?

【问题讨论】:

    标签: listbox selection hittest rubber-band


    【解决方案1】:

    您可以使用此代码获取 UIElement 相对于另一个 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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      • 2014-09-21
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多