Search code examples
c#mvvmavaloniauiavalonia

How to implement MenuItem with InputGesture in an Avalonia MVVM Application?


I need a Menu with InputGesture like in the picture here: https://github.com/AvaloniaUI/Avalonia/pull/3602

Starting in Microsoft Visual Studio Community 2019 with a new Avalonia MVVM Application, I try to set up the XAML in Visual Studio to achieve this:

<MenuItem Header="New" InputGesture="CTRL + N" />

This line then results in the error message:

Unable to resolve suitable regular or attached property InputGesture on type Avalonia.Controls:Avalonia.Controls.MenuItem

I found a working sample here: https://github.com/AvaloniaUI/XamlControlsGallery

I'd try to build my Solution and Project based on that example to get it working. But I would prefer being able to implement it on a new Avalonia MVVM Application build from scratch with Visual Studio. Hints are very appreciated.


Solution

  • For the time being the new features are not documented in Avalonia Docs, it may be useful for others to see my findings.
    My goal was to create menus with shortcuts containing numbers (e.g. Alt+0).
    In pure WPF (without Avalonia) the value "Alt+0" can be assigned to the tags representing a menu with shortcut.
    In Avalonia however the values must correspond to the enum Key in Key.cs.
    Otherwise the mentioned example menu would display Alt+None instead of Alt+0, and command binding wouldn’t work as expected.
    This is an example XAML line to display the menu and bind the command as desired:

    <MenuItem Header="Menu 0" InputGesture="Alt+D0" HotKey="Alt+D0" Command="{Binding Alt0Command}"/>