10 changed files with 97 additions and 8 deletions
@ -0,0 +1,36 @@ |
|||
using System; |
|||
using Avalonia.Controls.Primitives; |
|||
using Avalonia.Interactivity; |
|||
using Avalonia.Styling; |
|||
|
|||
namespace Avalonia.Controls |
|||
{ |
|||
public class NativeMenuBar : TemplatedControl |
|||
{ |
|||
public static readonly AttachedProperty<bool> EnableMenuItemClickForwardingProperty = |
|||
AvaloniaProperty.RegisterAttached<NativeMenuBar, MenuItem, Boolean>( |
|||
"EnableMenuItemClickForwarding"); |
|||
|
|||
static NativeMenuBar() |
|||
{ |
|||
EnableMenuItemClickForwardingProperty.Changed.Subscribe(args => |
|||
{ |
|||
var item = (MenuItem)args.Sender; |
|||
if (args.NewValue.Equals(true)) |
|||
item.Click += OnMenuItemClick; |
|||
else |
|||
item.Click -= OnMenuItemClick; |
|||
}); |
|||
} |
|||
|
|||
public static void SetEnableMenuItemClickForwarding(MenuItem menuItem, bool enable) |
|||
{ |
|||
menuItem.SetValue(EnableMenuItemClickForwardingProperty, enable); |
|||
} |
|||
|
|||
private static void OnMenuItemClick(object sender, RoutedEventArgs e) |
|||
{ |
|||
(((MenuItem)sender).DataContext as NativeMenuItem)?.RaiseClick(); |
|||
} |
|||
} |
|||
} |
|||
@ -1,7 +1,10 @@ |
|||
namespace Avalonia.Controls |
|||
using System; |
|||
|
|||
namespace Avalonia.Controls |
|||
{ |
|||
public class NativeMenuItemSeperator : NativeMenuItemBase |
|||
{ |
|||
|
|||
[Obsolete("This is a temporary hack to make our MenuItem recognize this as a separator, don't use", true)] |
|||
public string Header => "-"; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,20 @@ |
|||
using System; |
|||
using System.Globalization; |
|||
using Avalonia.Data.Converters; |
|||
|
|||
namespace Avalonia.Themes.Default |
|||
{ |
|||
class InverseBooleanValueConverter : IValueConverter |
|||
{ |
|||
public bool Default { get; set; } |
|||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) |
|||
{ |
|||
return value is bool b ? !b : Default; |
|||
} |
|||
|
|||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) |
|||
{ |
|||
return value is bool b ? !b : !Default; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,25 @@ |
|||
<Style xmlns="https://github.com/avaloniaui" |
|||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
|||
xmlns:local="clr-namespace:Avalonia.Themes.Default" |
|||
Selector="NativeMenuBar"> |
|||
<Style.Resources> |
|||
<local:InverseBooleanValueConverter x:Key="AvaloniaThemesDefaultNativeMenuBarInverseBooleanValueConverter" Default="True"/> |
|||
</Style.Resources> |
|||
<Setter Property="Template"> |
|||
<ControlTemplate> |
|||
<Menu |
|||
IsVisible="{Binding $parent[TopLevel].(NativeMenu.IsNativeMenuExported), Converter={StaticResource AvaloniaThemesDefaultNativeMenuBarInverseBooleanValueConverter}}" |
|||
Items="{Binding $parent[TopLevel].(NativeMenu.Menu).Items}"> |
|||
<Menu.Styles> |
|||
<Style Selector="MenuItem"> |
|||
<Setter Property="Header" Value="{Binding Header}"/> |
|||
<Setter Property="Items" Value="{Binding Menu.Items}"/> |
|||
<Setter Property="Command" Value="{Binding Command}"/> |
|||
<Setter Property="CommandParameter" Value="{Binding CommandParameter}"/> |
|||
<Setter Property="(NativeMenuBar.EnableMenuItemClickForwarding)" Value="True"/> |
|||
</Style> |
|||
</Menu.Styles> |
|||
</Menu> |
|||
</ControlTemplate> |
|||
</Setter> |
|||
</Style> |
|||
Loading…
Reference in new issue