From 75652d094e3fc4c364c828ff60eca2c266378b93 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 9 Apr 2020 14:33:42 -0300 Subject: [PATCH] fix direct property implementations on nativemenu item. --- src/Avalonia.Controls/NativeMenu.cs | 2 - src/Avalonia.Controls/NativeMenuItem.cs | 61 ++++++++++++------------- 2 files changed, 28 insertions(+), 35 deletions(-) diff --git a/src/Avalonia.Controls/NativeMenu.cs b/src/Avalonia.Controls/NativeMenu.cs index 54aa2b5e3d..7c06111a01 100644 --- a/src/Avalonia.Controls/NativeMenu.cs +++ b/src/Avalonia.Controls/NativeMenu.cs @@ -3,8 +3,6 @@ using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using Avalonia.Collections; -using Avalonia.Data; -using Avalonia.LogicalTree; using Avalonia.Metadata; namespace Avalonia.Controls diff --git a/src/Avalonia.Controls/NativeMenuItem.cs b/src/Avalonia.Controls/NativeMenuItem.cs index c1144d45b2..1033a0bfe6 100644 --- a/src/Avalonia.Controls/NativeMenuItem.cs +++ b/src/Avalonia.Controls/NativeMenuItem.cs @@ -10,6 +10,7 @@ namespace Avalonia.Controls private string _header; private KeyGesture _gesture; private bool _enabled = true; + private ICommand _command; private NativeMenu _menu; @@ -55,13 +56,7 @@ namespace Avalonia.Controls } public static readonly DirectProperty MenuProperty = - AvaloniaProperty.RegisterDirect(nameof(Menu), o => o._menu, - (o, v) => - { - if (v.Parent != null && v.Parent != o) - throw new InvalidOperationException("NativeMenu already has a parent"); - o._menu = v; - }); + AvaloniaProperty.RegisterDirect(nameof(Menu), o => o.Menu, (o, v) => o.Menu = v); public NativeMenu Menu { @@ -75,38 +70,26 @@ namespace Avalonia.Controls } public static readonly DirectProperty HeaderProperty = - AvaloniaProperty.RegisterDirect(nameof(Header), o => o._header, (o, v) => o._header = v); + AvaloniaProperty.RegisterDirect(nameof(Header), o => o.Header, (o, v) => o.Header = v); public string Header { - get => GetValue(HeaderProperty); - set => SetValue(HeaderProperty, value); + get => _header; + set => SetAndRaise(HeaderProperty, ref _header, value); } public static readonly DirectProperty GestureProperty = - AvaloniaProperty.RegisterDirect(nameof(Gesture), o => o._gesture, (o, v) => o._gesture = v); + AvaloniaProperty.RegisterDirect(nameof(Gesture), o => o.Gesture, (o, v) => o.Gesture = v); public KeyGesture Gesture { - get => GetValue(GestureProperty); - set => SetValue(GestureProperty, value); - } - - private ICommand _command; + get => _gesture; + set => SetAndRaise(GestureProperty, ref _gesture, value); + } public static readonly DirectProperty CommandProperty = AvaloniaProperty.RegisterDirect(nameof(Command), - o => o._command, (o, 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(); - }); + o => o.Command, (o, v) => o.Command = v); /// /// Defines the property. @@ -115,13 +98,12 @@ namespace Avalonia.Controls Button.CommandParameterProperty.AddOwner(); public static readonly DirectProperty EnabledProperty = - AvaloniaProperty.RegisterDirect(nameof(Enabled), o => o._enabled, - (o, v) => o._enabled = v, true); + AvaloniaProperty.RegisterDirect(nameof(Enabled), o => o.Enabled, (o, v) => o.Enabled = v, true); public bool Enabled { - get => GetValue(EnabledProperty); - set => SetValue(EnabledProperty, value); + get => _enabled; + set => SetAndRaise(EnabledProperty, ref _enabled, value); } void CanExecuteChanged() @@ -133,8 +115,21 @@ namespace Avalonia.Controls public ICommand Command { - get => GetValue(CommandProperty); - set => SetValue(CommandProperty, value); + get => _command; + 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(); + } } ///