Skip to content

Commit

Permalink
(#3668) Changes after rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed May 1, 2021
1 parent 111400e commit 2867abe
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

<GroupBox Grid.Row="0"
Grid.Column="0"
Margin="4,2"
Margin="4 2"
Header="TextBox">
<AdornerDecorator>
<StackPanel>
Expand All @@ -73,7 +73,7 @@
<DataTemplate>
<StackPanel VerticalAlignment="Center" Orientation="Horizontal">
<iconPacks:PackIconMaterial VerticalAlignment="Center" Kind="FaceProfile" />
<TextBlock Margin="2,0,0,0"
<TextBlock Margin="2 0 0 0"
VerticalAlignment="Center"
Text="{Binding}" />
</StackPanel>
Expand Down Expand Up @@ -188,7 +188,7 @@

<GroupBox Grid.Row="0"
Grid.Column="1"
Margin="4,2"
Margin="4 2"
Header="RichTextBox">
<StackPanel>
<RichTextBox Margin="{StaticResource ControlMargin}"
Expand Down Expand Up @@ -265,7 +265,7 @@

<GroupBox Grid.Row="0"
Grid.Column="2"
Margin="4,2"
Margin="4 2"
Header="PasswordBox">
<StackPanel>
<PasswordBox Margin="{StaticResource ControlMargin}"
Expand Down Expand Up @@ -323,7 +323,7 @@

<GroupBox Grid.Row="0"
Grid.Column="3"
Margin="4,2"
Margin="4 2"
Grid.IsSharedSizeScope="True"
Header="NumericUpDown">
<GroupBox.Resources>
Expand Down Expand Up @@ -484,11 +484,9 @@
</mah:MetroHeader>


<Separator Margin="0,5" />
<Separator Margin="0 5" />


<mah:MetroHeader Grid.Row="1"
mah:HeaderedControlHelper.HeaderFontWeight="Bold"
<mah:MetroHeader mah:HeaderedControlHelper.HeaderFontWeight="Bold"
mah:HeaderedControlHelper.HeaderForeground="{DynamicResource MahApps.Brushes.Accent}"
Header="Non-Nullable">
<mah:NumericUpDown x:Name="NUD"
Expand All @@ -499,11 +497,10 @@
Value="{Binding NumericUpDownValue}" />
</mah:MetroHeader>

<mah:MetroHeader Grid.Row="1"
mah:HeaderedControlHelper.HeaderFontWeight="Bold"
<mah:MetroHeader mah:HeaderedControlHelper.HeaderFontWeight="Bold"
mah:HeaderedControlHelper.HeaderForeground="{DynamicResource MahApps.Brushes.Accent}"
Header="Nullable">
<mah:NumericUpDown x:Name="NUD_Nullable"
<mah:NumericUpDown x:Name="NUD_Nullable"
Margin="{StaticResource ControlMargin}"
mah:TextBoxHelper.ClearTextButton="{Binding ElementName=NUD, Path=(mah:TextBoxHelper.ClearTextButton)}"
mah:TextBoxHelper.Watermark="{Binding ElementName=NUD, Path=(mah:TextBoxHelper.Watermark)}"
Expand All @@ -523,6 +520,7 @@
SnapToMultipleOfInterval="{Binding ElementName=NUD, Path=SnapToMultipleOfInterval}"
Speedup="{Binding ElementName=NUD, Path=Speedup}"
StringFormat="{Binding ElementName=NUD, Path=StringFormat}"
SwitchUpDownButtons="{Binding ElementName=NUD, Path=SwitchUpDownButtons}"
TextAlignment="{Binding ElementName=NUD, Path=TextAlignment}"
Value="{Binding NullableNumericUpDownValue}" />
</mah:MetroHeader>
Expand All @@ -531,7 +529,7 @@

<GroupBox Grid.Row="1"
Grid.Column="0"
Margin="4,2"
Margin="4 2"
Header="HotKeyBox">
<StackPanel>
<CheckBox x:Name="ModifierKeysRequired"
Expand Down
67 changes: 33 additions & 34 deletions src/MahApps.Metro/Controls/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -378,32 +378,32 @@ private static void OnValuePropertyChanged(DependencyObject dependencyObject, De
}

[MustUseReturnValue]
private static object? CoerceValue(DependencyObject d, object? value)
private static object? CoerceValue(DependencyObject d, object? baseValue)
{
var numericUpDown = (NumericUpDown)d;
if (value is null)
if (baseValue is null)
{
return numericUpDown.DefaultValue;
}

double val = ((double?)value).Value;
var value = ((double?)baseValue).Value;

if (!numericUpDown.NumericInputMode.HasFlag(NumericInput.Decimal))
{
val = Math.Truncate(val);
value = Math.Truncate(value);
}

if (val < numericUpDown.Minimum)
if (value < numericUpDown.Minimum)
{
return numericUpDown.Minimum;
}

if (val > numericUpDown.Maximum)
if (value > numericUpDown.Maximum)
{
return numericUpDown.Maximum;
}

return val;
return value;
}

/// <summary>
Expand All @@ -418,26 +418,13 @@ public double? Value
set => this.SetValue(ValueProperty, value);
}


/// <summary>Identifies the <see cref="DefaultValue"/> dependency property.</summary>
public static readonly DependencyProperty DefaultValueProperty
= DependencyProperty.Register(nameof(DefaultValue),
typeof(double?),
typeof(NumericUpDown),
public static readonly DependencyProperty DefaultValueProperty
= DependencyProperty.Register(nameof(DefaultValue),
typeof(double?),
typeof(NumericUpDown),
new PropertyMetadata(null, OnDefaultValuePropertyChanged, CoerceDefaultValue));

/// <summary>
/// Gets or sets the default value of the NumericUpDown which will be used if the <see cref="Value"/> is <see langword="null"/>.
/// </summary>
[Bindable(true)]
[Category("Common")]
[DefaultValue(null)]
public double? DefaultValue
{
get { return (double?)GetValue(DefaultValueProperty); }
set { SetValue(DefaultValueProperty, value); }
}

private static void OnDefaultValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var numericUpDown = (NumericUpDown)d;
Expand All @@ -448,12 +435,13 @@ private static void OnDefaultValuePropertyChanged(DependencyObject d, Dependency
}
}

private static object CoerceDefaultValue(DependencyObject d, object value)
[MustUseReturnValue]
private static object? CoerceDefaultValue(DependencyObject d, object? baseValue)
{
if (value is double val)
if (baseValue is double val)
{
double minimum = ((NumericUpDown)d).Minimum;
double maximum = ((NumericUpDown)d).Maximum;
var minimum = ((NumericUpDown)d).Minimum;
var maximum = ((NumericUpDown)d).Maximum;

if (val < minimum)
{
Expand All @@ -464,10 +452,21 @@ private static object CoerceDefaultValue(DependencyObject d, object value)
return maximum;
}
}
return value;

return baseValue;
}

/// <summary>
/// Gets or sets the default value of the NumericUpDown which will be used if the <see cref="Value"/> is <see langword="null"/>.
/// </summary>
[Bindable(true)]
[Category("Common")]
[DefaultValue(null)]
public double? DefaultValue
{
get => (double?)this.GetValue(DefaultValueProperty);
set => this.SetValue(DefaultValueProperty, value);
}

/// <summary>Identifies the <see cref="Minimum"/> dependency property.</summary>
public static readonly DependencyProperty MinimumProperty
Expand Down Expand Up @@ -1487,7 +1486,7 @@ private void ChangeValueFromTextInput(TextBox? textBox)
}
else if (this.DefaultValue.HasValue)
{
this.SetValueTo(DefaultValue.Value);
this.SetValueTo(this.DefaultValue.Value);
}
}

Expand All @@ -1503,12 +1502,12 @@ private void OnTextChanged(object sender, TextChangedEventArgs e)

if (string.IsNullOrEmpty(((TextBox)sender).Text))
{
if (DefaultValue.HasValue)
if (this.DefaultValue.HasValue)
{
this.SetValueTo(DefaultValue.Value);
this.SetValueTo(this.DefaultValue.Value);
if (!this.manualChange)
{
this.InternalSetText(DefaultValue.Value);
this.InternalSetText(this.DefaultValue.Value);
}
}
else
Expand Down
4 changes: 0 additions & 4 deletions src/Mahapps.Metro.Tests/Tests/NumericUpDownTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ private static void SetText(TextBox theTextBox, string theText)
theTextBox.RaiseEvent(new RoutedEventArgs(UIElement.LostFocusEvent));
}


[Fact]
public async Task ShouldSetDefaultValue()
{
Expand All @@ -351,23 +350,20 @@ public async Task ShouldSetDefaultValue()

Assert.Equal(nud.Value, nud.DefaultValue);


// 2. Test: There is no default value, so we should be able to set it to null

nud.DefaultValue = null;
nud.Value = null;

Assert.Equal(nud.Value, nud.DefaultValue);


// 3. Test: We set the Default Value greater than the Maximum. It should be corrected by the control
nud.DefaultValue = 100;
nud.Value = null;

Assert.Equal(nud.Maximum, nud.DefaultValue);
Assert.Equal(nud.Maximum, nud.Value);


// 4. Test: We set the Default Value lower than the Minimum. It should be corrected by the control
nud.DefaultValue = -100;
nud.Value = null;
Expand Down

0 comments on commit 2867abe

Please sign in to comment.