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

MenuItems from ContextMenu.ItemTemplate disabled / greyed out when Command parameter is used #18032

Open
LazaroOnline opened this issue Jan 23, 2025 · 1 comment
Labels

Comments

@LazaroOnline
Copy link

Describe the bug

When a MenuItem Command is binded while in a ContextMenu.ItemTemplate, it seems to be disabled (gray out) and clicking does not work.
Note that all other values bind correctly from the same model, just not the command, and that the command binding is also compiled correctly.

The issue only occurs when ItemsSource with ItemTemplate is used, but if using static MenuItems it does work, example:

<ContextMenu x:DataType="model:User" ItemsSource="{Binding UserLinks}">
  <ContextMenu.ItemTemplate>
	<MenuItem Header="Test" Command="{Binding SomeFUnctionFromUserClass}/>
  </ContextMenu.Item
</ContextMenu>

But of course, static items cannot replace dynamic templates, so it is not a workaround.

To Reproduce

Checkout this simple example in this repo (in the branch issue-example-contextmenu-greyedout):

https://github.com/LazaroOnline/AvaloniaDataGridRowFlyoutRefresh-BugSample/tree/issue-example-contextmenu-greyedout

Run the app and try to right click on the DataGrid row to see the MenuItems that should not be disabled, but they are.

Here is a preview of the axaml from the repo:

<DataGrid.Styles>
  <Style Selector="DataGridRow">
	<Setter Property="ContextMenu">
	  <ContextMenu x:DataType="model:User" ItemsSource="{Binding UserLinks}">
		<ContextMenu.ItemTemplate>
		  <DataTemplate DataType="model:Link">
			<MenuItem Header="{Binding LinkName}" Command="{Binding OpenUrl}" />
		  </DataTemplate>
		</ContextMenu.ItemTemplate>
	  </ContextMenu>
	</Setter>
  </Style>
</DataGrid.Styles>

Expected behavior

No response

Avalonia version

11.2.3

OS

Windows

Additional context

No response

@LazaroOnline
Copy link
Author

Ok, I found the solution using... Styles.
this is confusing and hard to discover, lead me to a lot of frustration and thus many users are reporting these kind of things.

Here is the work-around:

<DataGrid.Styles>
  <Style Selector="DataGridRow">
    <Setter Property="ContextMenu">
      <ContextMenu x:DataType="model:User" ItemsSource="{Binding UserLinks}">
        <ContextMenu.Styles>
          <Style Selector="MenuItem" x:DataType="model:Link">
            <Setter Property="Header" Value="{Binding LinkName}"/>
            <Setter Property="Command" Value="{Binding OpenUrl}"/>
          </Style>
        </ContextMenu.Styles>
      </ContextMenu>
    </Setter>
  </Style>
</DataGrid.Styles>

I would say that using styles is a workaround but not a solution, the control should have a way to pass the ItemSource and MenuItems in a sane way a developer can use without going crazy...
Something like this should be possible:

<DataGrid ItemsSource="{Binding UserList}">
  <DataGrid.ContextMenu x:DataType="model:User" ItemsSource="{Binding UserLinks}">
    <MenuItem Header="LinkName" Binding="{Binding OpenUrl}" />
  </DataGrid.ContextMenu>
  ...
</DataGrid>

Data related properties like the Text/Content of a button and the Command should not be considered styles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant