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

feat(MenuFlyout): Add ItemContainerTheme #10020

Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 18 additions & 1 deletion src/Avalonia.Controls/Flyouts/MenuFlyout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Metadata;
using Avalonia.Styling;

namespace Avalonia.Controls
{
Expand All @@ -27,6 +28,12 @@ public MenuFlyout()
AvaloniaProperty.RegisterDirect<MenuFlyout, IDataTemplate?>(nameof(ItemTemplate),
x => x.ItemTemplate, (x, v) => x.ItemTemplate = v);

/// <summary>
/// Defines the <see cref="ItemContainerTheme"/> property.
/// </summary>
public static readonly StyledProperty<ControlTheme?> ItemContainerThemeProperty =
ItemsControl.ItemContainerThemeProperty.AddOwner<MenuFlyout>();

public Classes FlyoutPresenterClasses => _classes ??= new Classes();

/// <summary>
Expand All @@ -48,6 +55,15 @@ public IDataTemplate? ItemTemplate
set => SetAndRaise(ItemTemplateProperty, ref _itemTemplate, value);
}

/// <summary>
/// Gets or sets the <see cref="ControlTheme"/> that is applied to the container element generated for each item.
/// </summary>
public ControlTheme? ItemContainerTheme
{
get { return GetValue(ItemContainerThemeProperty); }
set { SetValue(ItemContainerThemeProperty, value); }
}

private Classes? _classes;
private IEnumerable? _items;
private IDataTemplate? _itemTemplate;
Expand All @@ -57,7 +73,8 @@ protected override Control CreatePresenter()
return new MenuFlyoutPresenter
{
[!ItemsControl.ItemsProperty] = this[!ItemsProperty],
[!ItemsControl.ItemTemplateProperty] = this[!ItemTemplateProperty]
[!ItemsControl.ItemTemplateProperty] = this[!ItemTemplateProperty],
[!ItemsControl.ItemContainerThemeProperty] = this[!ItemContainerThemeProperty],
};
}

Expand Down
10 changes: 10 additions & 0 deletions src/Avalonia.Controls/Flyouts/MenuFlyoutPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,15 @@ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e
}
}
}

protected internal override void PrepareContainerForItemOverride(Control element, object? item, int index)
{
base.PrepareContainerForItemOverride(element, item, index);

// Child menu items should not inherit the menu's ItemContainerTheme as that is specific
// for top-level menu items.
if ((element as MenuItem)?.ItemContainerTheme == ItemContainerTheme)
element.ClearValue(ItemContainerThemeProperty);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand.
When I set <MenuFlyout ItemContainerTheme="" /> I would expect same container theme to be applied on flyout items and subitems.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you. But I have decided to respect the same behavior of the menus.

protected internal override void PrepareContainerForItemOverride(Control element, object? item, int index)
{
base.PrepareContainerForItemOverride(element, item, index);
// Child menu items should not inherit the menu's ItemContainerTheme as that is specific
// for top-level menu items.
if ((element as MenuItem)?.ItemContainerTheme == ItemContainerTheme)
element.ClearValue(ItemContainerThemeProperty);
}

I've opened a discussion #10060 about it, but no one has responded yet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's different in case of Menu control.
As in Menu control you have top level menu items that are always rendered horizontally and flyout/popup menus with a different styles. And there you wouldn't expect same style to be applied everywhere.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally expected the same behavior also for the Menu as it happens in WPF. I'd like to do a PR that changes the behavior of the menus, but I'm waiting for your response first. I don't want invest time unnecessarily.

}
}
}