Browse Source

fixed some stuff I broke, and improved data type conversions

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
e04f959612
  1. 22
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Converters/InverseBoolConverter.cs
  2. 129
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/NumericUpDown.cs
  3. 25
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml
  4. 2
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/UpDownBase/UpDownBase.cs
  5. 7
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

22
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
}
}

129
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
}
}

25
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">
<!-- =============================================================================== -->
<!-- Common Converters -->
<!-- =============================================================================== -->
<coreConverters:InverseBoolConverter x:Key="InverseBoolConverter" />
<!-- =============================================================================== -->
<!-- Common Styles -->
<!-- =============================================================================== -->
@ -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 @@
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MaskedTextBox}">
<TextBox x:Name="TextBox"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontStretch="{TemplateBinding FontStretch}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding Foreground}"
MinWidth="20" AcceptsReturn="False" TextWrapping="NoWrap"
Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" />
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}}" />
</ControlTemplate>
</Setter.Value>
</Setter>

2
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/UpDownBase.cs → 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
{

7
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -76,6 +76,7 @@
<Compile Include="ButtonSpinner\SpinEventArgs.cs" />
<Compile Include="ButtonSpinner\Spinner.cs" />
<Compile Include="ButtonSpinner\ValidSpinDirections.cs" />
<Compile Include="Core\Converters\InverseBoolConverter.cs" />
<Compile Include="Magnifier\Converters\BorderThicknessToStrokeThicknessConverter.cs" />
<Compile Include="Magnifier\Converters\RadiusConverter.cs" />
<Compile Include="Magnifier\Magnifier.cs" />
@ -110,7 +111,7 @@
<Compile Include="RichTextBox\Formatters\RtfFormatter.cs" />
<Compile Include="RichTextBox\Formatters\XamlFormatter.cs" />
<Compile Include="RichTextBox\RichTextBox.cs" />
<Compile Include="Core\Primitives\UpDownBase.cs" />
<Compile Include="UpDownBase\UpDownBase.cs" />
<Compile Include="VisualStates.cs" />
<Compile Include="MessageBox\VisualStates.MessageBox.cs" />
<EmbeddedResource Include="Properties\Resources.resx">
@ -150,9 +151,7 @@
<ItemGroup>
<Resource Include="RichTextBoxFormatBar\Images\Underline16.png" />
</ItemGroup>
<ItemGroup>
<Folder Include="UpDownBase\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.

Loading…
Cancel
Save