Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions samples/ControlCatalog/Pages/ContextFlyoutPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@
IsHitTestVisible="False" />
</MenuItem.Icon>
</MenuItem>
<MenuItem GroupName="Radio-ableMenuItem"
Header="Menu Item with _RadioButton1"
IsChecked="True"
StaysOpenOnClick="True"
ToggleType="Radio" />
<MenuItem GroupName="Radio-ableMenuItem"
Header="Menu Item with _RadioButton2"
StaysOpenOnClick="True"
ToggleType="Radio" />
</MenuFlyout>
</Border.ContextFlyout>
<TextBlock Text="Defined in XAML" />
Expand Down
9 changes: 9 additions & 0 deletions samples/ControlCatalog/Pages/ContextMenuPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@
IsHitTestVisible="False" />
</MenuItem.Icon>
</MenuItem>
<MenuItem GroupName="Radio-ableMenuItem"
Header="Menu Item with _RadioButton1"
IsChecked="True"
StaysOpenOnClick="True"
ToggleType="Radio" />
<MenuItem GroupName="Radio-ableMenuItem"
Header="Menu Item with _RadioButton2"
StaysOpenOnClick="True"
ToggleType="Radio" />
<MenuItem Header="Menu Item that won't close on click" StaysOpenOnClick="True" />
</ContextMenu>
</Border.ContextMenu>
Expand Down
16 changes: 15 additions & 1 deletion src/Avalonia.Controls/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Reactive;
using Avalonia.VisualTree;

namespace Avalonia.Controls
{
Expand Down Expand Up @@ -387,7 +388,9 @@ public string? GroupName
/// <inheritdoc/>
IEnumerable<IMenuItem> IMenuElement.SubItems => LogicalChildren.OfType<IMenuItem>();

private IMenuInteractionHandler? MenuInteractionHandler => this.FindLogicalAncestorOfType<MenuBase>()?.InteractionHandler;
private IMenuInteractionHandler? MenuInteractionHandler =>
this.FindLogicalAncestorOfType<MenuBase>()?.InteractionHandler ??
this.FindAncestorOfType<MenuBase>()?.InteractionHandler;

/// <summary>
/// Opens the submenu.
Expand Down Expand Up @@ -470,6 +473,7 @@ protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
base.OnAttachedToVisualTree(e);

TryUpdateCanExecute();
RegisterInMenuInteractionHandler();
}

protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
Expand Down Expand Up @@ -908,6 +912,16 @@ private void PopupClosed(object? sender, EventArgs e)
SelectedItem = null;
}

private void RegisterInMenuInteractionHandler()
{
if (ToggleType != MenuItemToggleType.Radio || MenuInteractionHandler is not DefaultMenuInteractionHandler handler)
{
return;
}

handler.OnGroupOrTypeChanged(this, null);
}

void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => CanExecuteChangedHandler(sender, e);

void IClickableControl.RaiseClick()
Expand Down
17 changes: 17 additions & 0 deletions src/Avalonia.Controls/RadioButtonGroupManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ public void Add(IRadioButton radioButton)
_registeredGroups.Add(groupName, group);
}

int i = 0;
while (i < group.Count)
{
if (!group[i].TryGetTarget(out var current))
{
group.RemoveAt(i);
continue;
}

if (current == radioButton)
{
return;
}

i++;
}

group.Add(new WeakReference<IRadioButton>(radioButton));
}
}
Expand Down
41 changes: 41 additions & 0 deletions tests/Avalonia.Controls.UnitTests/MenuItemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,47 @@ public void Radio_MenuItem_In_Same_Group_Is_Unchecked()
Assert.True(menuItem3.IsChecked);
}

[Fact]
public void Radio_MenuItem_In_Same_Group_In_MenuFlyout_Is_Unchecked()
{
using var app = Application();

var menuItem1 = new MenuItem
{
GroupName = "A",
IsChecked = true,
StaysOpenOnClick = true,
ToggleType = MenuItemToggleType.Radio,
};
var menuItem2 = new MenuItem
{
GroupName = "A",
IsChecked = false,
StaysOpenOnClick = true,
ToggleType = MenuItemToggleType.Radio,
};

var flyout = new MenuFlyout
{
Items =
{
menuItem1,
menuItem2,
}
};
var button = new Button { ContextFlyout = flyout };
var window = new Window { Content = button };

window.Show();
flyout.ShowAt(button);
Dispatcher.UIThread.RunJobs(DispatcherPriority.Loaded, TestContext.Current.CancellationToken);

menuItem2.IsChecked = true;

Assert.False(menuItem1.IsChecked);
Assert.True(menuItem2.IsChecked);
}

[Fact]
public void Radio_Menu_Group_Can_Be_Changed_In_Runtime()
{
Expand Down