Menuitem greyed when command attribute is set
See original GitHub issueUsing VS2022 on Windows 10 21H2 with Avalonia Toolkit and Avalonia for VS2022 installed.
A MenuItem with Command={Binding MainWindow.OnOpenCommand}
is greyed out.
MenuItem myMenu = this.FindControl<MenuItem>("FileOpen");
returns myMenu as expected but the Command property is null.
An oven ready VS2022 project demonstrating the issues/questions is here: https://github.com/g40/AvaloniaMenu
XAML:
<Window 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="AvaloniaApplication1.MainWindow"
Title="AvaloniaApplication1">
<DockPanel VerticalAlignment="Top">
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Name="FileOpen" IsEnabled="True" Header="_Open..." Command="{Binding OpenCommand}" CommandParameter="{Binding ElementName=MainWindow}"/><Separator/>
<MenuItem Header="_Exit"/>
</MenuItem>
<MenuItem Header="_Edit" >
<MenuItem Header="Copy"/>
<MenuItem Header="Paste"/>
</MenuItem>
</Menu>
</DockPanel>
</Window
C# code
using Avalonia.Controls;
using System.Windows.Input;
using System.Reactive;
using System;
namespace AvaloniaApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// yep, this works. we find the control
MenuItem myMenu = this.FindControl<MenuItem>("FileOpen");
// how to wire this up in code ????
// myMenu.Command = openCommand;
}
//
public bool CanExitCommand()
{
return true;
}
public void OnExitCommand()
{
int i = 0;
}
//
public bool CanOpenCommand()
{
return true;
}
public void OnOpenCommand()
{
int i = 0;
}
}
}
Many thanks for listening.
Issue Analytics
- State:
- Created 6 months ago
- Comments:9 (5 by maintainers)
Top Results From Across the Web
WPF MenuItem grayed out when binding Command
From this article: Another solution is to manually bind the CommandTarget to the parent ContextMenu.
Read more >WPF Menu with submenu Greyed out Disabled
As you use the command binding, you are handling the event System. ... (the command should not execute), the bound menu item is...
Read more >Can't change the state of a menu item - Visual C++
You cannot change the state of a menu item from its command user-interface handler if the menu is attached to a dialog box...
Read more >HTML <menuitem> disabled Attribute
The disabled menu item is unclickable and usually, looks greyish in color. Syntax: <menuitem disabled>. Note: The menuitem disabled Attribute is ...
Read more ><menuitem> - HTML: HyperText Markup Language | MDN
Boolean attribute which indicates that the command is not available in the current state. Note that disabled is distinct from hidden ; the ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The binding is not correct. You should check the debug log, there should be a binding error. You can try to bind to {Binding $parent[Window].OnOpenCommand}
see #10787