Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 21 additions & 2 deletions MahApps.Metro/Controls/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ private static void InterceptManualEnterChangedCallback(DependencyObject depende
typeof(NumericUpDown),
new FrameworkPropertyMetadata(true, OnHasDecimalsChanged));

public static readonly DependencyProperty HasScientificNotationProperty = DependencyProperty.Register(
"HasScientificNotation",
typeof(bool),
typeof(NumericUpDown),
new PropertyMetadata(true));

private static readonly Regex RegexStringFormatHexadecimal = new Regex(@"^(?<complexHEX>.*{\d:X\d+}.*)?(?<simpleHEX>X\d+)?$", RegexOptions.Compiled);

private const double DefaultInterval = 1d;
Expand Down Expand Up @@ -463,6 +469,18 @@ public bool HasDecimals
set { SetValue(HasDecimalsProperty, value); }
}

/// <summary>
/// Indicates if the NumericUpDown should show the decimal separator or not.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should be update to match the correct behavior.

/// </summary>
[Bindable(true)]
[Category("Common")]
[DefaultValue(true)]
public bool HasScientificNotation
{
get { return (bool)GetValue(HasScientificNotationProperty); }
set { SetValue(HasScientificNotationProperty, value); }
}

/// <summary>
/// Called when this element or any below gets focus.
/// </summary>
Expand Down Expand Up @@ -704,13 +722,14 @@ protected void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
else if (textBox.SelectionStart > 0)
{
string elementBeforeCaret = textBox.Text.ElementAt(textBox.SelectionStart - 1).ToString(equivalentCulture);
if (elementBeforeCaret.Equals(ScientificNotationChar, StrComp))
if (elementBeforeCaret.Equals(ScientificNotationChar, StrComp) && HasScientificNotation)
{
e.Handled = false;
}
}
}
else if (text.Equals(ScientificNotationChar, StrComp) &&
else if (HasScientificNotation &&
text.Equals(ScientificNotationChar, StrComp) &&
textBox.SelectionStart > 0 &&
!textBox.Text.Any(i => i.ToString(equivalentCulture).Equals(ScientificNotationChar, StrComp)))
{
Expand Down
11 changes: 11 additions & 0 deletions samples/MetroDemo/ExampleViews/TextExamples.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,16 @@
Margin="1"
Content="HasDecimals"
IsChecked="True" />
<CheckBox x:Name="HasScientificNotationCheckBox"
Margin="1"
Content="HasScientificNotation"
IsChecked="True" />
</UniformGrid>

<Label Content="Min=&quot;0&quot;, Max=&quot;10&quot;, TextAlignment=&quot;Left&quot;" />
<Controls:NumericUpDown x:Name="Test"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsEnabled="False"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Maximum="10"
Expand All @@ -261,6 +266,7 @@
<Controls:NumericUpDown Controls:TextBoxHelper.ClearTextButton="True"
ButtonsAlignment="Left"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
Interval="5"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
IsTabStop="False"
Expand All @@ -269,13 +275,15 @@
<Controls:NumericUpDown Margin="{StaticResource ControlMargin}"
Controls:TextBoxHelper.Watermark="No Speedup when long pressed"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Speedup="false" />
<Controls:NumericUpDown Margin="{StaticResource ControlMargin}"
Controls:TextBoxHelper.UseFloatingWatermark="True"
Controls:TextBoxHelper.Watermark="Speedup when pressed after 1000ms"
Delay="1000"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Speedup="true" />

Expand All @@ -284,6 +292,7 @@
ContentStringFormat="StringFormat, Real Value = {0}" />
<Controls:NumericUpDown x:Name="test"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Maximum="100"
StringFormat="pcs. {0:N2} pcs."
Expand Down Expand Up @@ -315,6 +324,7 @@
<Controls:NumericUpDown x:Name="StringFormatNumUpDown"
Margin="{StaticResource ControlMargin}"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
Interval=".1"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
StringFormat="0,000.00"
Expand All @@ -324,6 +334,7 @@
<Controls:NumericUpDown Controls:TextBoxHelper.SelectAllOnFocus="True"
ButtonsAlignment="Left"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
Interval="5"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
IsTabStop="False"
Expand Down