Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding commands for submenu items makes whole submenu grayed out #3100

Closed
x2bool opened this issue Oct 11, 2019 · 16 comments
Closed

Binding commands for submenu items makes whole submenu grayed out #3100

x2bool opened this issue Oct 11, 2019 · 16 comments
Assignees
Labels
by-design The behavior reported in the issue is actually correct.
Projects

Comments

@x2bool
Copy link

x2bool commented Oct 11, 2019

<ContextMenu>
                
    <MenuItem
        Header="Activate"
        Command="{Binding ActivateCommand}">
        
        <MenuItem.Icon>
            <Image
                Classes="Icon"
                Source="Activate.png" />
        </MenuItem.Icon>
        
    </MenuItem>
    
    <MenuItem
        Header="Commands"
        Items="{Binding MenuItems}">
        
        <MenuItem.Icon>
            <Image
                Classes="Icon"
                Source="resm:Command.png" />
        </MenuItem.Icon>
        
        <MenuItem.Styles>
            <Style Selector="MenuItem">
                <Setter Property="Icon">
                    <Template>
                        <Image
                            Classes="Icon"
                            Source="resm:Command.png" />
                    </Template>
                </Setter>
                <Setter Property="Header" Value="{Binding Name}" />
                <!-- <Setter Property="Command" Value="{Binding SelectCommand}" /> -->
                <!-- <Setter Property="CommandParameter" Value="{Binding}" /> -->
            </Style>
        </MenuItem.Styles>
        
    </MenuItem>
    
</ContextMenu>

When SelectCommand is uncommented it makes whole "Commands" submenu disabled. Doesn't matter what value the SelectCommand has, even simple stub or null value grays out the submenu.

0.8.999 - binding disables the submenu

0.8 - binding works as expected and does not result in disabled menu

image

@jmacato jmacato added this to the 0.9 milestone Oct 11, 2019
@jmacato jmacato added this to To do in 0.9 Release via automation Oct 11, 2019
@grokys
Copy link
Member

grokys commented Oct 11, 2019

It sounds to me like SelectCommand can't be found, is this correct?

@x2bool
Copy link
Author

x2bool commented Oct 11, 2019

Sorry if the description is not clear. SelectCommand is bound to the items under "Commands" menu.

It is supposed to look like this:
image

Those submenu items have bindings to this model:

public class MenuItemModel
    {
        public string Name { get; set; }
        
        public ReactiveCommand<MenuItemModel, Unit> SelectCommand { get; set; }
            = ReactiveCommand.Create<MenuItemModel, Unit>(_ => Unit.Default);
    }

As you can observe on the screenshot it has Name and SelectCommand properties, where Name is the name of the command. In my example names are: "SCAN, SCAN MATCH, KEYS", and bindings to this property do work as they worked before. But when the binding to the SelectCommand appears whole menu is disabled for some reason, and submenu items are not accessible ar all

@x2bool
Copy link
Author

x2bool commented Oct 11, 2019

BTW can this be related to CommandProperty becoming direct property recently? It was StyledProperty not too long ago.

public static readonly DirectProperty<MenuItem, ICommand> CommandProperty =
            Button.CommandProperty.AddOwner<MenuItem>(
                menuItem => menuItem.Command, 
                (menuItem, command) => menuItem.Command = command, 
                enableDataValidation: true);

@danwalmsley danwalmsley moved this from To do to Punt to 0.9.1 in 0.9 Release Oct 16, 2019
@chhadidg
Copy link

chhadidg commented Jul 31, 2020

Question: Is this issue solved? Running Avalonia 0.9.11. MenuItem is grayed out and command is not working on a ContextMenu.

<MenuItem Header="{Binding ContextMenuAddSpecNode, FallbackValue=Add}" Command="{Binding ProjectContextMenuAddSpecNode}" CommandParameter="{Binding ElementName=MainWindow}"></MenuItem>

Have the same code on a normal Menu and it works just fine.

kindest
Christian

@maxkatz6
Copy link
Member

@chhadidg issue is not closed, so it probably is not solved.
But you can check it with 0.10.0-preview2 nuget version just to be sure.

@chhadidg
Copy link

chhadidg commented Jul 31, 2020

Thanks. Is there any other way to execute a command from a contextmenu menuitem? Just started with Avalonia.

@Kermel
Copy link

Kermel commented Dec 4, 2020

Hi, any news on this topic ?

@lordzomat
Copy link

I have the same issue with the latest stable version and the following context menu. I cannot get it to work, is there a known workaround?

<ContextMenu IsVisible="{Binding IsRootNode}">
	<MenuItem Header="resx:ContextMenuRemoveRootNode" Command="{Binding RemoveRootNodeCommand}"></MenuItem>
</ContextMenu>

Testing with 0.10.0-rc1 is not possible at the moment because it crashes with a StackOverflowException which i could not fix

@lordzomat
Copy link

I created a small test application using the latest 0.10.0-rc1 and the error is still there. Not able to get the context menu to work.

@Kermel
Copy link

Kermel commented Dec 23, 2020

For now, I am using this solution (hereby an example from my menu with the option to switch between languages):

<Menu>
   <MenuItem Header="Some menu item"/>
   <MenuItem Header="Options">      
       <MenuItem Header="Languages" Items="{Binding LanguageMenuItems}">
         <MenuItem.Styles>
           <Style Selector="MenuItem">
             <Setter Property="Icon" Value="{Binding Icon}"/>
             <Setter Property="Header" Value="{Binding LanguageCode}"/>
             <Setter Property="Command" Value="{Binding $parent[UserControl].DataContext.SwitchLanguageCommand}"/>
             <Setter Property="CommandParameter" Value="{Binding LanguageCode}"/>
           </Style>
         </MenuItem.Styles>          
       </MenuItem>  
  </MenuItem>
</Menu>

notes:

  • the whole menu is placed in my "TopMenu.axaml" user control and is using a TopMenuViewModel. This view model contains the collection of LanguageMenuItems (my instances of a simple "LanguageMenuItemModel"s with properties used in the Setters (Icon, LanguageCode))
  • The working trick here is the command "SwitchLanguageCommand" I am using to bind on the menu item is also declared in the TopMenuViewModel (declaring it on the LanguageMenuItem doesn't work for some strange reason

@maxkatz6
Copy link
Member

maxkatz6 commented Jan 2, 2021

@Kermel @x2bool
I took a look on this issue with your examples and found, that there is no any actual bug, but only unclear behavior.
When you set Style to the parent <MenuItem> element (Commands or Languages in your examples), style is also applied on this item itself. And it can't find command in the parent DataContext, so it shows disabled menu.

It can be fixed with more specific Selector, that will not match parent item, but only its children. In my example "SubItems" class will filter items:

       <MenuItem Header="Languages" Items="{Binding LanguageMenuItems}" Classes="SubItems">
         <MenuItem.Styles>
           <Style Selector="MenuItem.SubItems MenuItem">
             <Setter Property="Icon" Value="{Binding Icon}"/>
             <Setter Property="Header" Value="{Binding LanguageCode}"/>
             <Setter Property="Command" Value="{Binding SwitchLanguageCommand}"/>
             <Setter Property="CommandParameter" Value="{Binding LanguageCode}"/>
           </Style>
         </MenuItem.Styles>          
       </MenuItem>  

@maxkatz6
Copy link
Member

maxkatz6 commented Jan 2, 2021

@lordzomat I am not sure if you have same issue. But I already see, you have created another one - #5212

@maxkatz6
Copy link
Member

maxkatz6 commented Jan 2, 2021

I am not sure if this issue can be closed as "by design" or can we do something with this to make behavior more clear.
@grokys FYI

@lordzomat
Copy link

@maxkatz6 i can reproduce the issue using the latest version with the test application mentioned in issue #5212
i have no style defined on that so it must be caused by another thing? If you want i can provide you the application.

@mstumpf585
Copy link

Is it possible to add @maxkatz6 example to https://avaloniaui.net/docs/controls/menu ?

@Kermel
Copy link

Kermel commented Feb 11, 2021

@Kermel @x2bool
I took a look on this issue with your examples and found, that there is no any actual bug, but only unclear behavior.
When you set Style to the parent <MenuItem> element (Commands or Languages in your examples), style is also applied on this item itself. And it can't find command in the parent DataContext, so it shows disabled menu.

It can be fixed with more specific Selector, that will not match parent item, but only its children. In my example "SubItems" class will filter items:

       <MenuItem Header="Languages" Items="{Binding LanguageMenuItems}" Classes="SubItems">
         <MenuItem.Styles>
           <Style Selector="MenuItem.SubItems MenuItem">
             <Setter Property="Icon" Value="{Binding Icon}"/>
             <Setter Property="Header" Value="{Binding LanguageCode}"/>
             <Setter Property="Command" Value="{Binding SwitchLanguageCommand}"/>
             <Setter Property="CommandParameter" Value="{Binding LanguageCode}"/>
           </Style>
         </MenuItem.Styles>          
       </MenuItem>  

Thank you, this helped solving my problem !
It would be good to add this to the available examples... :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
by-design The behavior reported in the issue is actually correct.
Projects
No open projects
0.9 Release
  
Punt to 0.9.1
Development

No branches or pull requests

8 participants