Search code examples
avalonia

Mouse events are not triggered in Avalonia user control


I'm trying to add Pan and Zoom to custom user control in Avalonia 0.10.10 using mouse wheel and mouse move events.

Standard avalonia template

dotnet new avalonia.mvvm -o Demo

User Control XAML

<UserControl 
  xmlns="https://github.com/avaloniaui"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  x:Class="CrossCanvas.Views.Avalonia.CanvasView">
</UserControl>

User Control

public partial class CanvasView : UserControl // Canvas
{
  public CanvasView()
  {
    AvaloniaXamlLoader.Load(this);

    PointerMoved += OnMouseMove;
    PointerWheelChanged += OnWheel;
  }

  protected void OnWheel(object sender, PointerWheelEventArgs e) {}
  protected void OnMouseMove(object sender, PointerEventArgs e) {}
}

Code that was used as an example

https://github.com/AvaloniaUI/Avalonia/issues/2492#issuecomment-489898224

The issue

No matter where I add new event handlers, in the constructor or in EndInit handler, these events are not being triggered. Maybe Avalonia 0.10.10 had some breaking changes or I do something wrong?


Solution

  • UserControl is invisible for hit test (pointer input) for obvious reasons to avoid it being blocking input from any other control behind it. But you can easily make it visible for hit test by making it "visible" - set Background to non-null value. For example, "Transparent".