diff --git a/src/MahApps.Metro/Controls/NumericUpDown.cs b/src/MahApps.Metro/Controls/NumericUpDown.cs index 84a3cc497..527283212 100644 --- a/src/MahApps.Metro/Controls/NumericUpDown.cs +++ b/src/MahApps.Metro/Controls/NumericUpDown.cs @@ -365,6 +365,28 @@ public bool InterceptManualEnter set => this.SetValue(InterceptManualEnterProperty, BooleanBoxes.Box(value)); } + /// Identifies the dependency property. + public static readonly DependencyProperty SyncTextWithValueWhileEditingProperty + = DependencyProperty.Register(nameof(SyncTextWithValueWhileEditing), + typeof(bool), + typeof(NumericUpDown), + new PropertyMetadata(BooleanBoxes.FalseBox)); + + /// + /// Gets or sets a value indicating whether the displayed text is kept in sync with the + /// while the control is being edited (has keyboard focus), when the value + /// is changed from an external source such as a binding. When set to + /// (the default), the current behavior is kept: the text is only refreshed from the value once + /// the control loses focus. + /// + [Category("Behavior")] + [DefaultValue(false)] + public bool SyncTextWithValueWhileEditing + { + get => (bool)this.GetValue(SyncTextWithValueWhileEditingProperty); + set => this.SetValue(SyncTextWithValueWhileEditingProperty, BooleanBoxes.Box(value)); + } + /// Identifies the dependency property. public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(nameof(Value), @@ -1238,6 +1260,22 @@ protected virtual void OnValueChanged(double? oldValue, double? newValue) this.InternalSetText(newValue); } } + else if (this.SyncTextWithValueWhileEditing && this.valueTextBox != null) + { + var textRepresentsValue = newValue.HasValue + && this.ValidateText(this.valueTextBox.Text, out var textValue) + && FormattedValue(textValue, this.StringFormat, this.SpecificCultureInfo).IsCloseTo(newValue.Value); + + if (!textRepresentsValue) + { + this.InternalSetText(newValue); + + if (this.valueTextBox.IsKeyboardFocused) + { + this.valueTextBox.SelectAll(); + } + } + } this.EnableDisableUpDown();