diff --git a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml index 5f7355d..75baa53 100644 --- a/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml +++ b/demo/Ursa.Demo/Pages/NumericUpDownDemo.axaml @@ -22,7 +22,7 @@ - + + diff --git a/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs b/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs index aa8abee..663b5f1 100644 --- a/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs +++ b/demo/Ursa.Demo/ViewModels/NumericUpDownDemoViewModel.cs @@ -1,6 +1,7 @@ using Avalonia.Controls; using Avalonia.Layout; using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; using System; using System.Globalization; using Ursa.Controls; @@ -39,6 +40,17 @@ 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) + { + CommandUpdateText = $"Command Exe,CommandParameter={v}"; + } + public NumericUpDownDemoViewModel() { @@ -65,7 +77,7 @@ public partial class NumericUpDownDemoViewModel : ObservableObject partial void OnValueChanging(uint oldValue, uint newValue) { - Console.WriteLine(oldValue); + // Console.WriteLine(oldValue); } } \ No newline at end of file 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); }