WPF: Adorner Hit Testing / MouseDown Event

I have an Adorner that adorns the border (see screenshot below). The MouseDown event for Adorner, however, only grows when an element is clicked in adorner. I need the MouseDown event to be raised when I click anywhere in adorner above a decorated item. How can this be done? Should I add a transparent control to adorner or is there another way to do this? Thanks for any help!

Screenshot and draft VS 2008: http://cid-0432ee4cfe9c26a0.skydrive.live.com/browse.aspx/%C3%96ffentlich?uc=2

Code for Advertiser:

class myAdorner : Adorner { public myAdorner(UIElement element) : base(element) { this.MouseDown += new System.Windows.Input.MouseButtonEventHandler(myAdorner_MouseDown); } void myAdorner_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e) { MessageBox.Show("ok"); } // Draws two rectangles: one in the upper-left and another one in the lower-right corner protected override void OnRender(System.Windows.Media.DrawingContext drawingContext) { Size size = this.AdornedElement.RenderSize; Rect r1 = new Rect(0.5, 0.5, 20, 20); Rect r4 = new Rect(size.Width - 20.5, size.Height - 20.5, 20, 20); SolidColorBrush brush = new SolidColorBrush(Colors.AliceBlue); Pen pen = new Pen(Brushes.Black, 1); drawingContext.DrawRectangle(brush, pen, r1); drawingContext.DrawRectangle(brush, pen, r4); } } 
+6
c # wpf adorner
source share
2 answers

When I did this in the past, I always used a transparent container. This is not enough to have an empty brush; you really need to use color # 00000000 (or some other color alpha 0). You can disable IsHitTestVisible for items inside the container so that the container receives all mouse events.

+3
source share

So the problem is that your adorner can only trigger mouse events where there are visible elements in your adorner ... two squares in the corner.

If you want to listen to mouseevents throughout the entire element that you decorate, you need to register AdornedElement.PreviewMouseDown. This will give your adorner the opportunity to do his work before the MouseDown event is fired with a decorated element.

+1
source share

All Articles