ContextMenu.ItemsSource binding acts as OneTime even if specified otherwise #18444
Replies: 4 comments
-
Found related problem: it seems the properties like IsVisible or IsEnabled also has only one-time binding for ContextMenu/MenuItem which is odd. Temporarily I solved that by switching from ContextMenu to MenuFlyout, binding there works correctly. But still, there is a problem with ContextMenu. |
Beta Was this translation helpful? Give feedback.
-
@anovik I'm not seeing correct bindings, even with MenuFlyout - the getter is still only called once at the beginning. Can you share your snippet? Here's what I tried changing mine to: <Button>
<Button.ContextFlyout>
<MenuFlyout ItemsSource="{Binding Items, Mode=TwoWay}"><!--I tried multiple "Modes" here-->
<MenuItem Header="{Binding Header}" Command="{Binding ActionCommand}"/>
</MenuFlyout>
</Button.ContextFlyout>
</Button> |
Beta Was this translation helpful? Give feedback.
-
Ok, ChatGPT to the rescue. I added private void ContextMenu_AttachedToVisualTree(object sender, VisualTreeAttachmentEventArgs e)
{
mainViewModel.ContextMenuItems.Clear();
foreach (.....)
{
mainViewModel.ContextMenuItems.Add(.....);
}
} And that seems to dynamically work for updating the items every time. Definitely a workaround - wish I didn't have to use the code-behind for this |
Beta Was this translation helpful? Give feedback.
-
"OneWay" doesn't mean that it will be called each time control is displayed. |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
I have bound a ContextMenu.ItemsSource property to a list in my viewmodel, but the getter is only called once, even if I specify a different binding mode such as OneWay or TwoWay, the bindind still acts as OneTime
To Reproduce
MainWindow.axaml:
MainViewModel.cs:
Expected behavior
I would expect the getter for
Items
to be called every time the context menu is opened, but it is notAvalonia version
11.1.0
OS
Windows
Additional context
No response
Beta Was this translation helpful? Give feedback.
All reactions