question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Menuitem greyed when command attribute is set

See original GitHub issue

Using 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:closed
  • Created 6 months ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
rabbitismcommented, Mar 23, 2023

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}

0reactions
timuniecommented, Mar 27, 2023

see #10787

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found