From 849ae9e302a5f54ee8ca76ea56b937e9c87ef351 Mon Sep 17 00:00:00 2001 From: heartacker Date: Mon, 25 Mar 2024 23:49:22 +0800 Subject: [PATCH 1/4] Command --- .../NumericUpDown/NumericUpDownBase.cs | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs index 40df9e3..956e065 100644 --- a/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs +++ b/src/Ursa/Controls/NumericUpDown/NumericUpDownBase.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Globalization; using System.Net.Mime; +using System.Windows.Input; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; @@ -464,6 +465,33 @@ public abstract class NumericUpDownBase : NumericUpDown where T : struct, ICo set => SetValue(EmptyInputValueProperty, value); } + + public static readonly StyledProperty CommandProperty = AvaloniaProperty.Register( + nameof(Command)); + + public ICommand? Command + { + get => GetValue(CommandProperty); + set => SetValue(CommandProperty, value); + } + + public static readonly StyledProperty CommandParameterProperty = + AvaloniaProperty.Register(nameof(CommandParameter)); + + public object? CommandParameter + { + get => this.GetValue(CommandParameterProperty); + set => this.SetValue(CommandParameterProperty, value); + } + + private void InvokeCommand(object? cp) + { + if (this.Command != null && this.Command.CanExecute(cp)) + { + this.Command.Execute(cp); + } + } + /// /// Defines the event. /// @@ -504,11 +532,17 @@ public abstract class NumericUpDownBase : NumericUpDown where T : struct, ICo if (IsInitialized) { SyncTextAndValue(false, null, true); + SetValidSpinDirection(); + T? oldValue = args.GetOldValue(); + T? newValue = args.GetNewValue(); + var e = new ValueChangedEventArgs(ValueChangedEvent, oldValue, newValue); + RaiseEventCommand(e); } - SetValidSpinDirection(); - T? oldValue = args.GetOldValue(); - T? newValue = args.GetNewValue(); - var e = new ValueChangedEventArgs(ValueChangedEvent, oldValue, newValue); + } + + private void RaiseEventCommand(ValueChangedEventArgs e) + { + InvokeCommand(this.CommandParameter ?? e.NewValue); RaiseEvent(e); } From ecf207c651592edf4ca43ca1d9e5b5aae603ba66 Mon Sep 17 00:00:00 2001 From: heartacker Date: Mon, 25 Mar 2024 23:49:49 +0800 Subject: [PATCH 2/4] demo --- demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml | 1 + .../ViewModels/NumericUpDownDemoViewModel.cs | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml index 5f7355d..51d5a89 100644 --- a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml +++ b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml @@ -25,6 +25,7 @@ Date: Tue, 26 Mar 2024 07:01:30 +0800 Subject: [PATCH 3/4] demo to update text --- demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml | 7 ++++++- demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs | 4 +++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml index 51d5a89..5a0f79c 100644 --- a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml +++ b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml @@ -25,12 +25,12 @@ + diff --git a/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs index a533528..663b5f1 100644 --- a/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs @@ -40,13 +40,15 @@ public partial class NumericUpDownDemoViewModel : ObservableObject [ObservableProperty] private bool _IsEnable = true; + [ObservableProperty] private string _CommandUpdateText = "Command not Execute"; + uint v = 0; [RelayCommand] // void Trythis() void Trythis(uint v) // void Trythis(object v) { - System.Diagnostics.Trace.WriteLine(v); + CommandUpdateText = $"Command Exe,CommandParameter={v}"; } From 492d7956844bb8d27393b410adfbb0eee010872e Mon Sep 17 00:00:00 2001 From: heartacker Date: Tue, 26 Mar 2024 07:24:00 +0800 Subject: [PATCH 4/4] https://github.com/irihitech/Ursa.Avalonia/issues/179 --- demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml index 5a0f79c..75baa53 100644 --- a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml +++ b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml @@ -22,7 +22,7 @@ - +