如何将不可见图层添加到一行

时间:2011-08-27 04:11:43

标签: wpf click line shape stroke

我有一个线形,我已经实现了双击,但线条太薄了。我想在它周围添加一个透明的白色衬垫,这样就不必在线上完全点击它。

我真的不想增加笔触粗细,我希望它保持形状,因为我不想把它放在内容控件或边框中。

3 个答案:

答案 0 :(得分:5)

这样做的一种方法是覆盖此行的标准Hit Testing。不幸的是,WPF的Line类是密封的,我个人认为这是犯罪行为: - )

这是一段代码,用于再现Line行为,但在另一个类中,定义了Tolerance属性(默认值为5)。随意测试一下:

using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace MyNamespace
{
    public class HitTolerantLine : Shape
    {
        public static readonly DependencyProperty X1Property = DependencyProperty.Register("X1", typeof(double), typeof(Line), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(IsDoubleFinite));
        public static readonly DependencyProperty X2Property = DependencyProperty.Register("X2", typeof(double), typeof(Line), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(IsDoubleFinite));
        public static readonly DependencyProperty Y1Property = DependencyProperty.Register("Y1", typeof(double), typeof(Line), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(IsDoubleFinite));
        public static readonly DependencyProperty Y2Property = DependencyProperty.Register("Y2", typeof(double), typeof(Line), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure), new ValidateValueCallback(IsDoubleFinite));
        public static readonly DependencyProperty ToleranceProperty = DependencyProperty.Register("Tolerance", typeof(double), typeof(Line), new FrameworkPropertyMetadata(5.0), new ValidateValueCallback(IsDoubleFinite));

        private LineGeometry _geometry;
        private static readonly Pen _strokePen;

        static HitTolerantLine()
        {
            _strokePen = new Pen(Brushes.Black, 1.0);
            _strokePen.Freeze();
        }

        protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
        {
            HitTestResult res = base.HitTestCore(hitTestParameters);

            // didn't hit? let's add some tolerance
            if ((res == null) && (_geometry != null) && (Tolerance > 0))
            {
                if (_geometry.StrokeContains(_strokePen, hitTestParameters.HitPoint, Tolerance, ToleranceType.Absolute))
                {
                    res = new PointHitTestResult(this, hitTestParameters.HitPoint);
                }
            }
            return res;
        }

        protected virtual void DefineGeometry()
        {
            Point startPoint = new Point(X1, Y1);
            Point endPoint = new Point(X2, Y2);
            _geometry = new LineGeometry(startPoint, endPoint);
        }

        protected override Size MeasureOverride(Size constraint)
        {
            DefineGeometry();
            return base.MeasureOverride(constraint);
        }

        protected static bool IsDoubleFinite(object o)
        {
            double d = (double)o;
            return (!double.IsInfinity(d) && !double.IsNaN(d));
        }

        protected override Geometry DefiningGeometry
        {
            get
            {
                return _geometry;
            }
        }

        public double Tolerance
        {
            get
            {
                return (double)base.GetValue(ToleranceProperty);
            }
            set
            {
                base.SetValue(ToleranceProperty, value);
            }
        }

        [TypeConverter(typeof(LengthConverter))]
        public double X1
        {
            get
            {
                return (double) base.GetValue(X1Property);
            }
            set
            {
                base.SetValue(X1Property, value);
            }
        }

        [TypeConverter(typeof(LengthConverter))]
        public double X2
        {
            get
            {
                return (double) base.GetValue(X2Property);
            }
            set
            {
                base.SetValue(X2Property, value);
            }
        }

        [TypeConverter(typeof(LengthConverter))]
        public double Y1
        {
            get
            {
                return (double) base.GetValue(Y1Property);
            }
            set
            {
                base.SetValue(Y1Property, value);
            }
        }

        [TypeConverter(typeof(LengthConverter))]
        public double Y2
        {
            get
            {
                return (double) base.GetValue(Y2Property);
            }
            set
            {
                base.SetValue(Y2Property, value);
            }
        }
    }
}

答案 1 :(得分:1)

填充区域无法点击。

我能想到的唯一方法,考虑到你的限制是增加笔画粗细并将笔画笔管理为透明色和可见色的渐变

答案 2 :(得分:0)

在顶部绘制一条带有较大StrokeThickness的透明线,并对其应用双击行为。