Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/MahApps.Metro/Controls/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,28 @@ public bool InterceptManualEnter
set => this.SetValue(InterceptManualEnterProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="SyncTextWithValueWhileEditing"/> dependency property.</summary>
public static readonly DependencyProperty SyncTextWithValueWhileEditingProperty
= DependencyProperty.Register(nameof(SyncTextWithValueWhileEditing),
typeof(bool),
typeof(NumericUpDown),
new PropertyMetadata(BooleanBoxes.FalseBox));

/// <summary>
/// Gets or sets a value indicating whether the displayed text is kept in sync with the
/// <see cref="Value"/> 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 <see langword="false"/>
/// (the default), the current behavior is kept: the text is only refreshed from the value once
/// the control loses focus.
/// </summary>
[Category("Behavior")]
[DefaultValue(false)]
public bool SyncTextWithValueWhileEditing
{
get => (bool)this.GetValue(SyncTextWithValueWhileEditingProperty);
set => this.SetValue(SyncTextWithValueWhileEditingProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="Value"/> dependency property.</summary>
public static readonly DependencyProperty ValueProperty
= DependencyProperty.Register(nameof(Value),
Expand Down Expand Up @@ -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();

Expand Down
Loading