Skip to content
Merged
Show file tree
Hide file tree
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
75 changes: 40 additions & 35 deletions src/MahApps.Metro/MahApps.Metro.Shared/Controls/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ public class NumericUpDown : Control
typeof(NumericUpDown),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.Inherits, IsReadOnlyPropertyChangedCallback));

private static void IsReadOnlyPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue && e.NewValue != null)
{
var numUpDown = (NumericUpDown)dependencyObject;
numUpDown.ToggleReadOnlyMode((bool)e.NewValue | !numUpDown.InterceptManualEnter);
}
}

public static readonly DependencyProperty StringFormatProperty = DependencyProperty.Register(
"StringFormat",
typeof(string),
Expand Down Expand Up @@ -126,15 +117,6 @@ private static void IsReadOnlyPropertyChangedCallback(DependencyObject dependenc
typeof(NumericUpDown),
new PropertyMetadata(true, InterceptManualEnterChangedCallback));

private static void InterceptManualEnterChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue && e.NewValue != null)
{
var numUpDown = (NumericUpDown)dependencyObject;
numUpDown.ToggleReadOnlyMode(!(bool)e.NewValue | numUpDown.IsReadOnly);
}
}

public static readonly DependencyProperty CultureProperty = DependencyProperty.Register(
"Culture",
typeof(CultureInfo),
Expand Down Expand Up @@ -173,6 +155,26 @@ private static void InterceptManualEnterChangedCallback(DependencyObject depende
typeof(NumericUpDown),
new PropertyMetadata(default(bool), OnSnapToMultipleOfIntervalChanged));

private static void IsReadOnlyPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue && e.NewValue != null)
{
var numUpDown = (NumericUpDown)dependencyObject;
var isReadOnly = (bool)e.NewValue;
numUpDown.ToggleReadOnlyMode(isReadOnly || !numUpDown.InterceptManualEnter);
}
}

private static void InterceptManualEnterChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != e.NewValue && e.NewValue != null)
{
var numUpDown = (NumericUpDown)dependencyObject;
var interceptManualEnter = (bool)e.NewValue;
numUpDown.ToggleReadOnlyMode(!interceptManualEnter || numUpDown.IsReadOnly);
}
}

private static readonly Regex RegexStringFormatHexadecimal = new Regex(@"^(?<complexHEX>.*{\d:X\d+}.*)?(?<simpleHEX>X\d+)?$", RegexOptions.Compiled);
private static readonly Regex RegexStringFormatNumber = new Regex(@"[-+]?(?<![0-9][.,])\b[0-9]+(?:[.,\s][0-9]+)*[.,]?[0-9]?(?:[eE][-+]?[0-9]+)?\b(?!\.[0-9])", RegexOptions.Compiled);

Expand Down Expand Up @@ -1164,27 +1166,30 @@ private void OnTextBoxLostFocus(object sender, RoutedEventArgs e)
return;
}

TextBox tb = (TextBox)sender;
_manualChange = false;

double convertedValue;
if (ValidateText(tb.Text, out convertedValue))
if (_manualChange)
{
if (SnapToMultipleOfInterval && Math.Abs(this.Interval) > 0)
{
convertedValue = Math.Round(convertedValue / Interval) * Interval;
}
TextBox tb = (TextBox)sender;
_manualChange = false;

if (convertedValue > Maximum)
{
convertedValue = this.Maximum;
}
else if (convertedValue < Minimum)
double convertedValue;
if (ValidateText(tb.Text, out convertedValue))
{
convertedValue = this.Minimum;
}
if (SnapToMultipleOfInterval && Math.Abs(this.Interval) > 0)
{
convertedValue = Math.Round(convertedValue / Interval) * Interval;
}

if (convertedValue > Maximum)
{
convertedValue = this.Maximum;
}
else if (convertedValue < Minimum)
{
convertedValue = this.Minimum;
}

SetCurrentValue(ValueProperty, convertedValue);
SetCurrentValue(ValueProperty, convertedValue);
}
}

OnValueChanged(Value, Value);
Expand Down
7 changes: 7 additions & 0 deletions src/MahApps.Metro/MahApps.Metro/Themes/NumericUpDown.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@
<Setter TargetName="PART_NumericDown" Property="IsEnabled" Value="False" />
<Setter TargetName="PART_NumericUp" Property="IsEnabled" Value="False" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsReadOnly" Value="False" />
<Condition Property="InterceptManualEnter" Value="False" />
</MultiTrigger.Conditions>
<Setter TargetName="PART_TextBox" Property="IsReadOnly" Value="True" />
</MultiTrigger>
<Trigger SourceName="PART_NumericUp" Property="IsMouseOver" Value="True">
<Setter TargetName="PART_NumericUp" Property="Background" Value="{DynamicResource GrayBrush8}" />
<Setter TargetName="PolygonUp" Property="Fill" Value="{DynamicResource AccentColorBrush}" />
Expand Down