Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some fixes in NumericUpDown #2245

Merged
merged 13 commits into from
Dec 22, 2015
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
14 changes: 0 additions & 14 deletions MahApps.Metro/Controls/DataGridNumericUpDownColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public class DataGridNumericUpDownColumn : DataGridBoundColumn
private string stringFormat = (string)NumericUpDown.StringFormatProperty.DefaultMetadata.DefaultValue;
private bool hideUpDownButtons = (bool)NumericUpDown.HideUpDownButtonsProperty.DefaultMetadata.DefaultValue;
private double upDownButtonsWidth = (double)NumericUpDown.UpDownButtonsWidthProperty.DefaultMetadata.DefaultValue;
private Binding foregroundBinding;

static DataGridNumericUpDownColumn()
{
Expand Down Expand Up @@ -117,26 +116,13 @@ private NumericUpDown GenerateNumericUpDown(bool isEditing, DataGridCell cell)
if (numericUpDown == null)
{
numericUpDown = new NumericUpDown();
// create binding to cell foreground to get changed brush from selection
foregroundBinding = new Binding("Foreground") { Source = cell, Mode = BindingMode.OneWay };
}

SyncProperties(numericUpDown);

ApplyStyle(isEditing, true, numericUpDown);
ApplyBinding(Binding, numericUpDown, NumericUpDown.ValueProperty);

if (!isEditing && Equals(this.Foreground, SystemColors.ControlTextBrush))
{
// bind to cell foreground to get changed brush from selection
ApplyBinding(foregroundBinding, numericUpDown, Control.ForegroundProperty);
}
else
{
// no foreground change for editing
BindingOperations.ClearBinding(numericUpDown, Control.ForegroundProperty);
}

numericUpDown.Minimum = Minimum;
numericUpDown.Maximum = Maximum;
numericUpDown.StringFormat = StringFormat;
Expand Down
111 changes: 54 additions & 57 deletions MahApps.Metro/Controls/Helper/TextBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,6 @@ public static void SetUseFloatingWatermark(DependencyObject obj, bool value)
obj.SetValue(UseFloatingWatermarkProperty, value);
}

private static void SetTextLength(DependencyObject obj, int value)
{
obj.SetValue(TextLengthProperty, value);
obj.SetValue(HasTextProperty, value >= 1);
}

/// <summary>
/// Gets if the attached TextBox has text.
/// </summary>
Expand All @@ -207,7 +201,7 @@ public static void SetHasText(DependencyObject obj, bool value)
obj.SetValue(HasTextProperty, value);
}

static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TextBox)
{
Expand Down Expand Up @@ -250,44 +244,67 @@ static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedE
{
var numericUpDown = d as NumericUpDown;
numericUpDown.SelectAllOnFocus = (bool)e.NewValue;
if ((bool)e.NewValue)
{
numericUpDown.ValueChanged += OnNumericUpDownValueChaged;
numericUpDown.GotFocus += NumericUpDownGotFocus;
}
else
{
numericUpDown.ValueChanged -= OnNumericUpDownValueChaged;
numericUpDown.GotFocus -= NumericUpDownGotFocus;
}
}
}

static void TextChanged(object sender, TextChangedEventArgs e)
private static void SetTextLength<TDependencyObject>(TDependencyObject sender, Func<TDependencyObject, int> funcTextLength) where TDependencyObject : DependencyObject
{
var txtBox = sender as TextBox;
if (txtBox == null)
return;
SetTextLength(txtBox, txtBox.Text.Length);
if (sender != null)
{
var value = funcTextLength(sender);
sender.SetValue(TextLengthProperty, value);
sender.SetValue(HasTextProperty, value >= 1);
}
}

static void PasswordChanged(object sender, RoutedEventArgs e)
private static void TextChanged(object sender, RoutedEventArgs e)
{
var passBox = sender as PasswordBox;
if (passBox == null)
return;
SetTextLength(passBox, passBox.Password.Length);
SetTextLength(sender as TextBox, textBox => textBox.Text.Length);
}

static void TextBoxGotFocus(object sender, RoutedEventArgs e)
private static void OnNumericUpDownValueChaged(object sender, RoutedEventArgs e)
{
var txtBox = sender as TextBox;
if (txtBox == null)
return;
if (GetSelectAllOnFocus(txtBox))
{
txtBox.Dispatcher.BeginInvoke((Action)(txtBox.SelectAll));
}
SetTextLength(sender as NumericUpDown, numericUpDown => numericUpDown.Value.HasValue ? 1 : 0);
}

private static void PasswordChanged(object sender, RoutedEventArgs e)
{
SetTextLength(sender as PasswordBox, passwordBox => passwordBox.Password.Length);
}

static void PasswordGotFocus(object sender, RoutedEventArgs e)
private static void TextBoxGotFocus(object sender, RoutedEventArgs e)
{
var passBox = sender as PasswordBox;
if (passBox == null)
return;
if (GetSelectAllOnFocus(passBox))
ControlGotFocus(sender as TextBox, textBox => textBox.SelectAll);
}

private static void NumericUpDownGotFocus(object sender, RoutedEventArgs e)
{
ControlGotFocus(sender as NumericUpDown, numericUpDown => numericUpDown.SelectAll);
}

private static void PasswordGotFocus(object sender, RoutedEventArgs e)
{
ControlGotFocus(sender as PasswordBox, passwordBox => passwordBox.SelectAll);
}

private static void ControlGotFocus<TDependencyObject>(TDependencyObject sender, Func<TDependencyObject, Action> funcSelectAll) where TDependencyObject : DependencyObject
{
if (sender != null)
{
passBox.Dispatcher.BeginInvoke((Action)(passBox.SelectAll));
if (GetSelectAllOnFocus(sender))
{
sender.Dispatcher.BeginInvoke(funcSelectAll, sender);
}
}
}

Expand Down Expand Up @@ -335,8 +352,6 @@ public static void SetIsClearTextButtonBehaviorEnabled(Button obj, bool value)
obj.SetValue(IsClearTextButtonBehaviorEnabledProperty, value);
}



public static ICommand GetButtonCommand(DependencyObject d)
{
return (ICommand)d.GetValue(ButtonCommandProperty);
Expand Down Expand Up @@ -444,22 +459,22 @@ private static void ButtonCommandOrClearTextChanged(DependencyObject d, Dependen
if (textbox != null)
{
// only one loaded event
textbox.Loaded -= TextBoxLoaded;
textbox.Loaded += TextBoxLoaded;
textbox.Loaded -= TextChanged;
textbox.Loaded += TextChanged;
if (textbox.IsLoaded)
{
TextBoxLoaded(textbox, new RoutedEventArgs());
TextChanged(textbox, new RoutedEventArgs());
}
}
var passbox = d as PasswordBox;
if (passbox != null)
{
// only one loaded event
passbox.Loaded -= PassBoxLoaded;
passbox.Loaded += PassBoxLoaded;
passbox.Loaded -= PasswordChanged;
passbox.Loaded += PasswordChanged;
if (passbox.IsLoaded)
{
PassBoxLoaded(passbox, new RoutedEventArgs());
PasswordChanged(passbox, new RoutedEventArgs());
}
}
var combobox = d as ComboBox;
Expand All @@ -483,23 +498,5 @@ static void ComboBoxLoaded(object sender, RoutedEventArgs e)
comboBox.SetValue(HasTextProperty, !string.IsNullOrWhiteSpace(comboBox.Text) || comboBox.SelectedItem != null);
}
}

static void PassBoxLoaded(object sender, RoutedEventArgs e)
{
var passbox = sender as PasswordBox;
if (passbox != null)
{
passbox.SetValue(HasTextProperty, !string.IsNullOrWhiteSpace(passbox.Password));
}
}

static void TextBoxLoaded(object sender, RoutedEventArgs e)
{
var textbox = sender as TextBox;
if (textbox != null)
{
textbox.SetValue(HasTextProperty, !string.IsNullOrWhiteSpace(textbox.Text));
}
}
}
}
44 changes: 37 additions & 7 deletions MahApps.Metro/Controls/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace MahApps.Metro.Controls
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
Expand Down Expand Up @@ -157,7 +158,9 @@ private static void InterceptManualEnterChangedCallback(DependencyObject depende
typeof(bool),
typeof(NumericUpDown),
new FrameworkPropertyMetadata(true, OnHasDecimalsChanged));


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

private const double DefaultInterval = 1d;
private const int DefaultDelay = 500;
private const string ElementNumericDown = "PART_NumericDown";
Expand Down Expand Up @@ -855,6 +858,7 @@ private static void OnMaximumChanged(DependencyObject d, DependencyPropertyChang
var numericUpDown = (NumericUpDown)d;

numericUpDown.CoerceValue(ValueProperty);
numericUpDown.Value = (double?)CoerceValue(numericUpDown, numericUpDown.Value);
numericUpDown.OnMaximumChanged((double)e.OldValue, (double)e.NewValue);
numericUpDown.EnableDisableUpDown();
}
Expand All @@ -865,6 +869,7 @@ private static void OnMinimumChanged(DependencyObject d, DependencyPropertyChang

numericUpDown.CoerceValue(ValueProperty);
numericUpDown.CoerceValue(MaximumProperty);
numericUpDown.Value = (double?)CoerceValue(numericUpDown, numericUpDown.Value);
numericUpDown.OnMinimumChanged((double)e.OldValue, (double)e.NewValue);
numericUpDown.EnableDisableUpDown();
}
Expand All @@ -886,6 +891,7 @@ private static void OnStringFormatChanged(DependencyObject d, DependencyProperty
{
nud.InternalSetText(nud.Value);
}
nud.HasDecimals = !RegexStringFormatHexadecimal.IsMatch((string)e.NewValue);
}

private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down Expand Up @@ -924,14 +930,9 @@ private void InternalSetText(double? newValue)
{
_valueTextBox.Text = newValue.Value.ToString(culture);
}
else if (!StringFormat.Contains("{"))
{
// then we may have a StringFormat of e.g. "N0"
_valueTextBox.Text = newValue.Value.ToString(StringFormat, culture);
}
else
{
_valueTextBox.Text = string.Format(culture, StringFormat, newValue.Value);
FormatValue(newValue, culture);
}

if ((bool)GetValue(TextBoxHelper.IsMonitoringProperty))
Expand All @@ -940,6 +941,35 @@ private void InternalSetText(double? newValue)
}
}

private void FormatValue(double? newValue, CultureInfo culture)
{
var match = RegexStringFormatHexadecimal.Match(StringFormat);
if (match.Success)
{
if (match.Groups["simpleHEX"].Success)
{
// HEX DOES SUPPORT INT ONLY.
_valueTextBox.Text = ((int)newValue.Value).ToString(match.Groups["simpleHEX"].Value, culture);
}
else if (match.Groups["complexHEX"].Success)
{
_valueTextBox.Text = string.Format(culture, match.Groups["complexHEX"].Value, (int)newValue.Value);
}
}
else
{
if (!StringFormat.Contains("{"))
{
// then we may have a StringFormat of e.g. "N0"
_valueTextBox.Text = newValue.Value.ToString(StringFormat, culture);
}
else
{
_valueTextBox.Text = string.Format(culture, StringFormat, newValue.Value);
}
}
}

private ScrollViewer TryFindScrollViewer()
{
_valueTextBox.ApplyTemplate();
Expand Down
Loading