WPF FrameworkElement not receiving Mouse input
Asked Answered
N

2

12

Trying to get OnMouse events appearing in a child FrameworkElement. The parent element is a Panel (and the Background property is not Null).

class MyFrameworkElement : FrameworkElement
{
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        // Trying to get here!
        base.OnMouseDown(e);
    }
}

public class MyPanel : Panel
{
    protected override void OnMouseDown(MouseButtonEventArgs e)
    {
        // This is OK
        base.OnMouseDown(e);
    }
}

OnMouse never gets called, event is always unhandled and Snoop tells me that the routed event only ever seems to get as far as the Panel element.

<Window 
  x:Class="WpfApplication5.Window1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:l="clr-namespace:WpfApplication5"
  Title="Window1" Height="300" Width="300">
  <Border x:Name="myBorder" Background="Red">
    <l:MyPanel x:Name="myPanel" Background="Transparent">
      <l:MyFrameworkElement x:Name="myFE"/>
    </l:MyPanel>
  </Border>
</Window>

Docs say that FrameworkElement handles Input, but why not in this scenario?

Nutriment answered 16/7, 2010 at 21:52 Comment(0)
A
23

OnMouseDown will only be called if your element responds to Hit Testing. See Hit Testing in the Visual Layer. The default implementation will do hit testing against the graphics drawn in OnRender. Creating a Panel with a Transparent background works because Panel draws a rectangle over its entire area, and that rectangle will catch the hit test. You can get the same effect by overriding OnRender to draw a transparent rectangle:

protected override void OnRender(DrawingContext drawingContext)
{
    drawingContext.DrawRectangle(Brushes.Transparent, null, 
        new Rect(0, 0, RenderSize.Width, RenderSize.Height));
}

You could also override HitTestCore so that all clicks are counted as hits:

protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
{
    return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
Antimatter answered 17/7, 2010 at 15:36 Comment(0)
M
1

I was able to reproduce the scenario you described. I did some playing around, and it wasn't until I changed the base class of MyFrameworkElement from FrameworkElement to something more concrete, like UserControl that events started firing like they should. I'm not 100% sure why this would be, but I would recommend using one of the classes derived from FrameworkElement that would suit your needs (like Panel, as you did in the example above, or Button).

I'd be curious to know the exact reason your example above produces these results...

Mord answered 16/7, 2010 at 22:47 Comment(1)
Thanks for your response. I've been playing around for a little bit and managed to get the behaviour I need by adding the following to FrameWorkElement: protected override void OnRender(DrawingContext drawingContext) { Size renderSize = this.RenderSize; Rect rect = new Rect(renderSize); Pen pen = new Pen(Brushes.Transparent, 0.0); drawingContext.DrawRectangle(Brushes.Transparent, pen, rect); } Perhaps FrameworkElement is zero in size unless something is added to it?Nutriment

© 2022 - 2024 — McMap. All rights reserved.