diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Converters/InverseBoolConverter.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Converters/InverseBoolConverter.cs new file mode 100644 index 00000000..10684dcb --- /dev/null +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Converters/InverseBoolConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Windows.Data; + +namespace Microsoft.Windows.Controls.Core.Converters +{ + public class InverseBoolConverter : IValueConverter + { + #region IValueConverter Members + + public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + return !(bool)value; + } + + public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) + { + throw new NotImplementedException(); + } + + #endregion + } +} diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs index a927b71d..715b7563 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs @@ -1,7 +1,6 @@ using System; using System.Windows; using System.Globalization; -using Microsoft.Windows.Controls.Primitives; namespace Microsoft.Windows.Controls { @@ -71,7 +70,7 @@ namespace Microsoft.Windows.Controls protected virtual void OnStringFormatChanged(string oldValue, string newValue) { - SyncTextAndValueProperties(InputBase.DisplayTextProperty, Value); + SyncTextAndValueProperties(NumericUpDown.DisplayTextProperty, Value); } #endregion //FormatString @@ -104,23 +103,21 @@ namespace Microsoft.Windows.Controls protected override object ConvertTextToValue(string text) { + object result = null; + NumberFormatInfo info = NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture); - if (text.Contains(info.PercentSymbol)) + + try { - if (ValueType == typeof(decimal)) - return TryParceDecimalPercent(text, info); - else - return TryParceDoublePercent(text, info); + result = FormatString.Contains("P") ? ParsePercent(text, ValueType, info) : ParseDataValue(text, ValueType, info); } - else + catch { - if (ValueType == typeof(decimal)) - return TryParceDecimal(text, info); - else if (ValueType == typeof(int)) - return TryParceInteger(text, info); - else - return TryParceDouble(text, info); + TextBox.Text = DisplayText = ConvertValueToText(Value); + return Value; } + + return result; } protected override string ConvertValueToText(object value) @@ -131,21 +128,13 @@ namespace Microsoft.Windows.Controls protected override void OnIncrement() { double newValue = (double)(Convert.ToDecimal(Value) + (decimal)Increment); - - if (ValueType != typeof(Double)) - Value = Convert.ChangeType(newValue, ValueType); - else - Value = newValue; + Value = ValueType != typeof(Double) ? Convert.ChangeType(newValue, ValueType) : newValue; } protected override void OnDecrement() { double newValue = (double)(Convert.ToDecimal(Value) - (decimal)Increment); - - if (ValueType != typeof(Double)) - Value = Convert.ChangeType(newValue, ValueType); - else - Value = newValue; + Value = ValueType != typeof(Double) ? Convert.ChangeType(newValue, ValueType) : newValue; } #endregion //Base Class Overrides @@ -175,58 +164,76 @@ namespace Microsoft.Windows.Controls } } - private double TryParceDoublePercent(string text, NumberFormatInfo info) + #region Parsing + + private static object ParseDataValue(string text, Type dataType, NumberFormatInfo info) { - double result; - text = text.Replace(info.PercentSymbol, null); - result = TryParceDouble(text, info); - return result / 100; + try + { + if (typeof(double) == dataType) + { + return ParseDouble(text, info); + } + else if (typeof(float) == dataType) + { + return ParseFloat(text, info); + } + else if (typeof(byte) == dataType || typeof(sbyte) == dataType || + typeof(short) == dataType || typeof(ushort) == dataType || typeof(Int16) == dataType || + typeof(int) == dataType || typeof(uint) == dataType || typeof(Int32) == dataType || + typeof(long) == dataType || typeof(ulong) == dataType || typeof(Int64) == dataType) + { + return ParseWholeNumber(text, dataType, info); + } + else if (typeof(decimal) == dataType) + { + return ParseDecimal(text, info); + } + else + { + throw new ArgumentException("Type not supported"); + } + } + catch (Exception ex) + { + throw; + } } - private decimal TryParceDecimalPercent(string text, NumberFormatInfo info) + private static double ParseDouble(string text, NumberFormatInfo info) { - decimal result; - text = text.Replace(info.PercentSymbol, null); - result = TryParceDecimal(text, info); - return result / 100; + return double.Parse(text, NumberStyles.Any, info); } - private decimal TryParceDecimal(string text, NumberFormatInfo info) + private static float ParseFloat(string text, NumberFormatInfo info) { - decimal result; - if (!decimal.TryParse(text, NumberStyles.Any, info, out result)) - { - //an error occured now lets reset our value - result = Convert.ToDecimal(Value); - TextBox.Text = DisplayText = ConvertValueToText(result); - } - return result; + double result = double.Parse(text, NumberStyles.Any, info); + return (float)result; } - private double TryParceDouble(string text, NumberFormatInfo info) + private static decimal ParseDecimal(string text, NumberFormatInfo info) { - double result; - if (!double.TryParse(text, NumberStyles.Any, info, out result)) - { - //an error occured now lets reset our value - result = Convert.ToDouble(Value); - TextBox.Text = DisplayText = ConvertValueToText(result); - } - return result; + return decimal.Parse(text, NumberStyles.Any, info); } - private int TryParceInteger(string text, NumberFormatInfo info) + private static object ParseWholeNumber(string text, Type dataType, NumberFormatInfo info) { - int result; - if (!int.TryParse(text, NumberStyles.Any, info, out result)) - { - //an error occured now lets reset our value - result = Convert.ToInt32(Value); - TextBox.Text = DisplayText = ConvertValueToText(result); - } - return result; + decimal result = decimal.Parse(text, NumberStyles.Any, info); + return Convert.ChangeType(result, dataType, info); } + private static object ParsePercent(string text, Type dataType, NumberFormatInfo info) + { + text = text.Replace(info.PercentSymbol, null); + + decimal result = decimal.Parse(text, NumberStyles.Any, info); + result = result / 100; + + return Convert.ChangeType(result, dataType, info); + } + + #endregion + #endregion //Methods } } diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml index 8e249531..aac4d182 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml @@ -2,8 +2,15 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:Microsoft.Windows.Controls" + xmlns:coreConverters="clr-namespace:Microsoft.Windows.Controls.Core.Converters" xmlns:magConverters="clr-namespace:Microsoft.Windows.Controls.Mag.Converters"> + + + + + + @@ -1094,6 +1101,7 @@ FontStyle="{TemplateBinding FontStyle}" FontWeight="{TemplateBinding FontWeight}" Foreground="{TemplateBinding Foreground}" + IsReadOnly="{Binding IsEditable, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}" MinWidth="20" AcceptsReturn="False" TextAlignment="Right" TextWrapping="NoWrap" Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" /> @@ -1112,14 +1120,15 @@ + FontFamily="{TemplateBinding FontFamily}" + FontSize="{TemplateBinding FontSize}" + FontStretch="{TemplateBinding FontStretch}" + FontStyle="{TemplateBinding FontStyle}" + FontWeight="{TemplateBinding FontWeight}" + Foreground="{TemplateBinding Foreground}" + IsReadOnly="{Binding IsEditable, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}" + MinWidth="20" AcceptsReturn="False" TextWrapping="NoWrap" + Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" /> diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/UpDownBase.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/UpDownBase/UpDownBase.cs similarity index 95% rename from ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/UpDownBase.cs rename to ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/UpDownBase/UpDownBase.cs index b0d0fe4b..25fc8410 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/UpDownBase.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/UpDownBase/UpDownBase.cs @@ -3,7 +3,7 @@ using System.Windows.Controls; using Microsoft.Windows.Controls.Primitives; using System.Windows.Input; -namespace Microsoft.Windows.Controls.Primitives +namespace Microsoft.Windows.Controls { public abstract class UpDownBase : InputBase { diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj index 43da33d6..2441b591 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj @@ -76,6 +76,7 @@ + @@ -110,7 +111,7 @@ - + @@ -150,9 +151,7 @@ - - - +