Skip to content

Commit

Permalink
Merge pull request #46 from heartacker/command
Browse files Browse the repository at this point in the history
add support command binding
  • Loading branch information
Dirkster99 authored Oct 20, 2022
2 parents 08e1b94 + a655ac5 commit 455d63f
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 7 deletions.
6 changes: 3 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
Please use the `IsUpdateValueWhenLostFocus` property.

- [support align the value to left/center/right #41](https://github.com/Dirkster99/NumericUpDownLib/pull/41)
Please use the HorizontalContentAlignment property.
Please use the `HorizontalContentAlignment` property.

- [add support to force trigger the ValueChanged event with ctrl+enter #39](https://github.com/Dirkster99/NumericUpDownLib/pull/39)
Please use Control+Enter to trigger a value changed event without having to actually change a value.
Please use <kbd>Control+Enter</kbd> to trigger a value changed event without having to actually change a value.
This shortcut cannot be configured out.

- [support disable editingTip Fixes #37 #43](https://github.com/Dirkster99/NumericUpDownLib/pull/43)
Please use the IsEnableValidatingIndicator property.
Please use the `EnableValidatingIndicator` property.

- [fix: the cursor will focus to the PART_Measuring_Element by mistake #44](https://github.com/Dirkster99/NumericUpDownLib/pull/44)

Expand Down
99 changes: 95 additions & 4 deletions source/NumericUpDownLib/Base/AbstractBaseUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace NumericUpDownLib.Base
[TemplatePart(Name = PART_MeasuringElement, Type = typeof(FrameworkElement))]
[TemplatePart(Name = PART_IncrementButton, Type = typeof(RepeatButton))]
[TemplatePart(Name = PART_DecrementButton, Type = typeof(RepeatButton))]
public abstract partial class AbstractBaseUpDown<T> : InputBaseUpDown
public abstract partial class AbstractBaseUpDown<T> : InputBaseUpDown, ICommandSource
{
#region fields
/// <summary>
Expand Down Expand Up @@ -255,6 +255,96 @@ public event RoutedPropertyChangedEventHandler<T> MaxValueChanged
}
#endregion events

#region Command

public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}

/// <summary>
/// Dependency property backing store for Command Value property.
/// </summary>
public static readonly DependencyProperty CommandProperty =
DependencyProperty.Register("Command", typeof(ICommand), typeof(AbstractBaseUpDown<T>),
new PropertyMetadata(null, new PropertyChangedCallback(CommandChangedCallBack)));

private static void CommandChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is AbstractBaseUpDown<T> nud)
{
ICommand oldCommand = e.OldValue as ICommand;
ICommand newCommand = e.NewValue as ICommand;
nud.HookUpCommand(oldCommand, newCommand);
}
}

/// <summary>
/// Dependency property backing store for CommandParameter Value property.
/// </summary>
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register("CommandParameter", typeof(object), typeof(AbstractBaseUpDown<T>));

public object CommandParameter
{
get { return (object)GetValue(CommandParameterProperty); }
set { SetValue(CommandParameterProperty, value); }
}

/// <summary>
/// Identifies the InputElement Dependency Property.
/// </summary>
public static readonly DependencyProperty InputElementProperty = DependencyProperty.Register("CommandTarget",
typeof(IInputElement), typeof(AbstractBaseUpDown<T>));

/// <summary>
/// Gets or sets the InputElement assigned to the control.
/// </summary>
public IInputElement CommandTarget
{
get { return (IInputElement)GetValue(InputElementProperty); }
set { SetValue(InputElementProperty, value); }
}

#region CommandHelper

private void CommandExecute(ICommand cmd)
{
if (cmd is RoutedCommand command)
command.Execute(CommandParameter, CommandTarget);
else if (cmd != null)
cmd.Execute(CommandParameter);
}

private void HookUpCommand(ICommand oldCommand, ICommand newCommand)
{
if (oldCommand != null)
{
oldCommand.CanExecuteChanged -= CanExecuteChanged;
}
if (newCommand != null)
{
newCommand.CanExecuteChanged += CanExecuteChanged;
}
}

private void CanExecuteChanged(object sender, EventArgs e)
{
if (this.Command is RoutedCommand command)
{
this.IsEnabled = command.CanExecute(CommandParameter, CommandTarget);
}
else if (this.Command != null)
{
this.IsEnabled = this.Command.CanExecute(CommandParameter);
}
}

#endregion

#endregion

#region properties
/// <summary>
/// Gets/sets whether the Increment or Decrement button is currently visible or not.
Expand Down Expand Up @@ -450,13 +540,13 @@ protected set
/// <summary>
/// Gets/sets the newest available value while inputting for data verification
/// </summary>
protected T LastEditingNumericValue
public T LastEditingNumericValue
{
get { return lastEditingNumericValue; }
set
protected set
{
lastEditingNumericValue = value;
if(EnableValidatingIndicator)
if (EnableValidatingIndicator)
EditingVisibility = lastEditingNumericValue.Equals(Value) ? Visibility.Hidden : Visibility.Visible;
}
}
Expand Down Expand Up @@ -1084,6 +1174,7 @@ protected virtual void OnValueChanged(RoutedPropertyChangedEventArgs<T> args)
{
_PART_TextBox.Text = FormatNumber(Value);
}
CommandExecute(Command);
this.RaiseEvent(args);
}

Expand Down

0 comments on commit 455d63f

Please sign in to comment.