diff --git a/Fluent.Ribbon/Controls/Spinner.cs b/Fluent.Ribbon/Controls/Spinner.cs index e2f35f514..c6c292633 100644 --- a/Fluent.Ribbon/Controls/Spinner.cs +++ b/Fluent.Ribbon/Controls/Spinner.cs @@ -12,6 +12,7 @@ using System.Windows.Input; using System.Windows.Markup; using System.Windows.Threading; + using Fluent.Converters; using Fluent.Internal; /// @@ -23,24 +24,16 @@ [TemplatePart(Name = "PART_ButtonDown", Type = typeof(RepeatButton))] public class Spinner : RibbonControl { - #region Events - /// /// Occurs when value has been changed /// public event RoutedPropertyChangedEventHandler ValueChanged; - #endregion - - #region Fields - // Parts of the control (must be in control template) private System.Windows.Controls.TextBox textBox; private RepeatButton buttonUp; private RepeatButton buttonDown; - #endregion - #region Properties #region Value @@ -91,8 +84,9 @@ private void ValueToTextBoxText() { if (this.IsTemplateValid()) { - this.textBox.Text = this.Value.ToString(this.Format, CultureInfo.CurrentCulture); - this.Text = this.textBox.Text; + var newText = (string)this.TextToValueConverter.ConvertBack(this.Value, typeof(string), this.Format, CultureInfo.CurrentCulture); + this.textBox.Text = newText; + this.Text = newText; } } @@ -315,6 +309,25 @@ public double InputWidth #endregion + #region TextToValueConverter + + /// + /// Gets or sets a converter which is used to convert from text to double and from double to text. + /// + public IValueConverter TextToValueConverter + { + get { return (IValueConverter)this.GetValue(TextToValueConverterProperty); } + set { this.SetValue(TextToValueConverterProperty, value); } + } + + /// + /// Using a DependencyProperty as the backing store for TextToValueConverter. This enables animation, styling, binding, etc... + /// + public static readonly DependencyProperty TextToValueConverterProperty = + DependencyProperty.Register(nameof(TextToValueConverter), typeof(IValueConverter), typeof(Spinner), new PropertyMetadata(new SpinnerTextToValueConverter())); + + #endregion TextToValueConverter + #endregion #region Constructors @@ -478,30 +491,9 @@ private void OnTextBoxPreviewKeyDown(object sender, KeyEventArgs e) private void TextBoxTextToValue() { - var text = this.textBox.Text; - - // Remove all except digits, signs and commas - var stringBuilder = new StringBuilder(); - - foreach (var symbol in text) - { - if (char.IsDigit(symbol) - || symbol == ',' - || symbol == '.' - || (symbol == '-' && stringBuilder.Length == 0)) - { - stringBuilder.Append(symbol); - } - } - - text = stringBuilder.ToString(); + var newValue = (double)this.TextToValueConverter.Convert(this.textBox.Text, typeof(double), this.Value, CultureInfo.CurrentCulture); - double value; - - if (double.TryParse(text, NumberStyles.Any, CultureInfo.CurrentCulture, out value)) - { - this.Value = GetLimitedValue(this, value); - } + this.Value = GetLimitedValue(this, newValue); this.ValueToTextBoxText(); } diff --git a/Fluent.Ribbon/Converters/SpinnerTextToValueConverter.cs b/Fluent.Ribbon/Converters/SpinnerTextToValueConverter.cs new file mode 100644 index 000000000..597c620e6 --- /dev/null +++ b/Fluent.Ribbon/Converters/SpinnerTextToValueConverter.cs @@ -0,0 +1,53 @@ +namespace Fluent.Converters +{ + using System; + using System.Globalization; + using System.Text; + using System.Windows.Data; + + public class SpinnerTextToValueConverter : IValueConverter + { + public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return this.TextToDouble((string)value, (double)parameter, culture); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return this.DoubleToText((double)value, (string)parameter, culture); + } + + public virtual double TextToDouble(string text, double previousValue, CultureInfo culture) + { + // Remove all except digits, signs and commas + var stringBuilder = new StringBuilder(); + + foreach (var symbol in text) + { + if (char.IsDigit(symbol) || + symbol == ',' || + symbol == '.' || + (symbol == '-' && stringBuilder.Length == 0)) + { + stringBuilder.Append(symbol); + } + } + + text = stringBuilder.ToString(); + + double doubleValue; + + if (double.TryParse(text, NumberStyles.Any, culture, out doubleValue) == false) + { + doubleValue = previousValue; + } + + return doubleValue; + } + + public virtual string DoubleToText(double value, string format, CultureInfo culture) + { + return value.ToString(format, culture); + } + } +} \ No newline at end of file diff --git a/Fluent.Ribbon/Fluent.Ribbon.NET 4.0.csproj b/Fluent.Ribbon/Fluent.Ribbon.NET 4.0.csproj index cad829a18..8dd8ea7cb 100644 --- a/Fluent.Ribbon/Fluent.Ribbon.NET 4.0.csproj +++ b/Fluent.Ribbon/Fluent.Ribbon.NET 4.0.csproj @@ -86,6 +86,7 @@ + diff --git a/Fluent.Ribbon/Fluent.Ribbon.NET 4.5.csproj b/Fluent.Ribbon/Fluent.Ribbon.NET 4.5.csproj index 2caa08290..c941e47b5 100644 --- a/Fluent.Ribbon/Fluent.Ribbon.NET 4.5.csproj +++ b/Fluent.Ribbon/Fluent.Ribbon.NET 4.5.csproj @@ -86,6 +86,7 @@ +