Adorner和Adorned元素上的事件

时间:2010-11-18 11:53:16

标签: wpf wpf-controls adorner

我有一个自定义线形,使用装饰器显示数组,并在该行的中间显示一些文字。

alt text

问题是装饰的行为与装饰元素无关,并且不会将事件“转移”给它。在下面的代码中,我被迫手动将adorner元素重新链接到装饰元素(ta.MouseLeftButtonDown += Adorner_MouseLeftButtonDown;),但不幸的是,即使这样也行不通...... 在调用this.OnMouseLeftButtonDown时,有人可以提出建议吗,为什么我没有收到相应的事件?

public class SegmentLine : Shape
{
    AdornerLayer aLayer;

    public static readonly DependencyProperty X1Property;
    public static readonly DependencyProperty X2Property;
    public static readonly DependencyProperty Y1Property;
    public static readonly DependencyProperty Y2Property;
    ...

    static SegmentLine() {
        X1Property = DependencyProperty.Register("X1", typeof(double), typeof(SegmentLine), new FrameworkPropertyMetadata(double.NaN,
            FrameworkPropertyMetadataOptions.AffectsRender));
        X2Pro...
    }

    public SegmentLine()
        : base()
    {
        this.Loaded += SegmentLine_Loaded;
    }

    void SegmentLine_Loaded(object sender, RoutedEventArgs e)
    {
        aLayer = AdornerLayer.GetAdornerLayer(this);
        if (aLayer != null)
        {
            TextAdorner ta = new TextAdorner(this);
            //ta.IsHitTestVisible = false;
            ta.MouseLeftButtonDown += Adorner_MouseLeftButtonDown;
            aLayer.Add(ta);
        }
    }

    void Adorner_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        // !! try to rise the MouseLeftButtonDown event
        this.OnMouseLeftButtonDown(e);
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        if (aLayer != null)
            aLayer.Update();
    }

    class TextAdorner : Adorner
    {
        public TextAdorner(UIElement adornedElement)
            : base(adornedElement) { }

        protected override void OnRender(DrawingContext drawingContext)
        {
            // ADD LABEL ...
            FormattedText ft = new FormattedText(...);
            drawingContext.DrawText(ft, midPoint);

            // Add ARROW ...
            var myPathGeometry = new PathGeometry { Figures = myPathFigureCollection };            
            drawingContext.DrawGeometry(Brushes.Black, null, myPathGeometry);
        }
    }

    protected override System.Windows.Media.Geometry DefiningGeometry
    {
        get
        {
            var geometryGroup = new GeometryGroup();
            // Add line
            geometryGroup.Children.Add(new LineGeometry(new Point(X1, Y1), new Point(X2, Y2)));
            return geometryGroup;
        }
    }    
}

2 个答案:

答案 0 :(得分:5)

您应该致电this.RaiseEvent(e);而不是this.OnMouseLeftButtonDown(e);将事件转移到装饰元素。
顺便说一句,您只能使用一个事件处理程序来传输多种事件类型

void SegmentLine_Loaded(object sender, RoutedEventArgs e)
{
    ..
    ta.MouseLeftButtonDown += Adorner_AnyEvent;
    ta.MouseRightButtonDown += Adorner_AnyEvent;
}

void Adorner_AnyEvent(object sender, RoutedEventArgs e)
{
    this.RaiseEvent(e);
}

答案 1 :(得分:1)

我知道这已经过时了,但是我有一个类似的问题(没有在Adorner中接收事件),我通过在我的OnRender中绘制透明背景来修复()

drawingContext.DrawRectangle(Brushes.Transparent, null, viewRect);

没有背景=没有事件。