Skip to content

Commit

Permalink
Fixes #205 by adding property TextToValueConverter of type IValueConv…
Browse files Browse the repository at this point in the history
…erter to Spinner
  • Loading branch information
batzen committed Dec 16, 2015
1 parent ba24517 commit 556c9d5
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 33 deletions.
58 changes: 25 additions & 33 deletions Fluent.Ribbon/Controls/Spinner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Threading;
using Fluent.Converters;
using Fluent.Internal;

/// <summary>
Expand All @@ -23,24 +24,16 @@
[TemplatePart(Name = "PART_ButtonDown", Type = typeof(RepeatButton))]
public class Spinner : RibbonControl
{
#region Events

/// <summary>
/// Occurs when value has been changed
/// </summary>
public event RoutedPropertyChangedEventHandler<double> 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
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -315,6 +309,25 @@ public double InputWidth

#endregion

#region TextToValueConverter

/// <summary>
/// Gets or sets a converter which is used to convert from text to double and from double to text.
/// </summary>
public IValueConverter TextToValueConverter
{
get { return (IValueConverter)this.GetValue(TextToValueConverterProperty); }
set { this.SetValue(TextToValueConverterProperty, value); }
}

/// <summary>
/// Using a DependencyProperty as the backing store for TextToValueConverter. This enables animation, styling, binding, etc...
/// </summary>
public static readonly DependencyProperty TextToValueConverterProperty =
DependencyProperty.Register(nameof(TextToValueConverter), typeof(IValueConverter), typeof(Spinner), new PropertyMetadata(new SpinnerTextToValueConverter()));

#endregion TextToValueConverter

#endregion

#region Constructors
Expand Down Expand Up @@ -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();
}
Expand Down
53 changes: 53 additions & 0 deletions Fluent.Ribbon/Converters/SpinnerTextToValueConverter.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions Fluent.Ribbon/Fluent.Ribbon.NET 4.0.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Controls\ComboBox.cs" />
<Compile Include="Controls\ContextMenu.cs" />
<Compile Include="Converters\ColorToSolidColorBrushConverter.cs" />
<Compile Include="Converters\SpinnerTextToValueConverter.cs" />
<Compile Include="IHeaderedControl.cs" />
<Compile Include="Internal\ItemsControlHelper.cs" />
<Compile Include="Internal\UIHelper.cs" />
Expand Down
1 change: 1 addition & 0 deletions Fluent.Ribbon/Fluent.Ribbon.NET 4.5.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Controls\ComboBox.cs" />
<Compile Include="Controls\ContextMenu.cs" />
<Compile Include="Converters\ColorToSolidColorBrushConverter.cs" />
<Compile Include="Converters\SpinnerTextToValueConverter.cs" />
<Compile Include="IHeaderedControl.cs" />
<Compile Include="Internal\ItemsControlHelper.cs" />
<Compile Include="Internal\UIHelper.cs" />
Expand Down

0 comments on commit 556c9d5

Please sign in to comment.