-
-
Notifications
You must be signed in to change notification settings - Fork 517
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #205 by adding property TextToValueConverter of type IValueConv…
…erter to Spinner
- Loading branch information
Showing
4 changed files
with
80 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters