Browse Source

fix direct property implementations on nativemenu item.

pull/3762/head
Dan Walmsley 7 years ago
parent
commit
75652d094e
  1. 2
      src/Avalonia.Controls/NativeMenu.cs
  2. 61
      src/Avalonia.Controls/NativeMenuItem.cs

2
src/Avalonia.Controls/NativeMenu.cs

@ -3,8 +3,6 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using Avalonia.Collections; using Avalonia.Collections;
using Avalonia.Data;
using Avalonia.LogicalTree;
using Avalonia.Metadata; using Avalonia.Metadata;
namespace Avalonia.Controls namespace Avalonia.Controls

61
src/Avalonia.Controls/NativeMenuItem.cs

@ -10,6 +10,7 @@ namespace Avalonia.Controls
private string _header; private string _header;
private KeyGesture _gesture; private KeyGesture _gesture;
private bool _enabled = true; private bool _enabled = true;
private ICommand _command;
private NativeMenu _menu; private NativeMenu _menu;
@ -55,13 +56,7 @@ namespace Avalonia.Controls
} }
public static readonly DirectProperty<NativeMenuItem, NativeMenu> MenuProperty = public static readonly DirectProperty<NativeMenuItem, NativeMenu> MenuProperty =
AvaloniaProperty.RegisterDirect<NativeMenuItem, NativeMenu>(nameof(Menu), o => o._menu, AvaloniaProperty.RegisterDirect<NativeMenuItem, NativeMenu>(nameof(Menu), o => o.Menu, (o, v) => o.Menu = v);
(o, v) =>
{
if (v.Parent != null && v.Parent != o)
throw new InvalidOperationException("NativeMenu already has a parent");
o._menu = v;
});
public NativeMenu Menu public NativeMenu Menu
{ {
@ -75,38 +70,26 @@ namespace Avalonia.Controls
} }
public static readonly DirectProperty<NativeMenuItem, string> HeaderProperty = public static readonly DirectProperty<NativeMenuItem, string> HeaderProperty =
AvaloniaProperty.RegisterDirect<NativeMenuItem, string>(nameof(Header), o => o._header, (o, v) => o._header = v); AvaloniaProperty.RegisterDirect<NativeMenuItem, string>(nameof(Header), o => o.Header, (o, v) => o.Header = v);
public string Header public string Header
{ {
get => GetValue(HeaderProperty); get => _header;
set => SetValue(HeaderProperty, value); set => SetAndRaise(HeaderProperty, ref _header, value);
} }
public static readonly DirectProperty<NativeMenuItem, KeyGesture> GestureProperty = public static readonly DirectProperty<NativeMenuItem, KeyGesture> GestureProperty =
AvaloniaProperty.RegisterDirect<NativeMenuItem, KeyGesture>(nameof(Gesture), o => o._gesture, (o, v) => o._gesture = v); AvaloniaProperty.RegisterDirect<NativeMenuItem, KeyGesture>(nameof(Gesture), o => o.Gesture, (o, v) => o.Gesture = v);
public KeyGesture Gesture public KeyGesture Gesture
{ {
get => GetValue(GestureProperty); get => _gesture;
set => SetValue(GestureProperty, value); set => SetAndRaise(GestureProperty, ref _gesture, value);
} }
private ICommand _command;
public static readonly DirectProperty<NativeMenuItem, ICommand> CommandProperty = public static readonly DirectProperty<NativeMenuItem, ICommand> CommandProperty =
AvaloniaProperty.RegisterDirect<NativeMenuItem, ICommand>(nameof(Command), AvaloniaProperty.RegisterDirect<NativeMenuItem, ICommand>(nameof(Command),
o => o._command, (o, v) => o => o.Command, (o, v) => o.Command = v);
{
if (o._command != null)
WeakSubscriptionManager.Unsubscribe(o._command,
nameof(ICommand.CanExecuteChanged), o._canExecuteChangedSubscriber);
o._command = v;
if (o._command != null)
WeakSubscriptionManager.Subscribe(o._command,
nameof(ICommand.CanExecuteChanged), o._canExecuteChangedSubscriber);
o.CanExecuteChanged();
});
/// <summary> /// <summary>
/// Defines the <see cref="CommandParameter"/> property. /// Defines the <see cref="CommandParameter"/> property.
@ -115,13 +98,12 @@ namespace Avalonia.Controls
Button.CommandParameterProperty.AddOwner<MenuItem>(); Button.CommandParameterProperty.AddOwner<MenuItem>();
public static readonly DirectProperty<NativeMenuItem, bool> EnabledProperty = public static readonly DirectProperty<NativeMenuItem, bool> EnabledProperty =
AvaloniaProperty.RegisterDirect<NativeMenuItem, bool>(nameof(Enabled), o => o._enabled, AvaloniaProperty.RegisterDirect<NativeMenuItem, bool>(nameof(Enabled), o => o.Enabled, (o, v) => o.Enabled = v, true);
(o, v) => o._enabled = v, true);
public bool Enabled public bool Enabled
{ {
get => GetValue(EnabledProperty); get => _enabled;
set => SetValue(EnabledProperty, value); set => SetAndRaise(EnabledProperty, ref _enabled, value);
} }
void CanExecuteChanged() void CanExecuteChanged()
@ -133,8 +115,21 @@ namespace Avalonia.Controls
public ICommand Command public ICommand Command
{ {
get => GetValue(CommandProperty); get => _command;
set => SetValue(CommandProperty, value); set
{
if (_command != null)
WeakSubscriptionManager.Unsubscribe(_command,
nameof(ICommand.CanExecuteChanged), _canExecuteChangedSubscriber);
SetAndRaise(CommandProperty, ref _command, value);
if (_command != null)
WeakSubscriptionManager.Subscribe(_command,
nameof(ICommand.CanExecuteChanged), _canExecuteChangedSubscriber);
CanExecuteChanged();
}
} }
/// <summary> /// <summary>

Loading…
Cancel
Save