Browse Source

fixed some stuff I broke, and improved data type conversions

pull/1645/head
brianlagunas_cp 16 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;
using System.Windows; using System.Windows;
using System.Globalization; using System.Globalization;
using Microsoft.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls namespace Microsoft.Windows.Controls
{ {
@ -71,7 +70,7 @@ namespace Microsoft.Windows.Controls
protected virtual void OnStringFormatChanged(string oldValue, string newValue) protected virtual void OnStringFormatChanged(string oldValue, string newValue)
{ {
SyncTextAndValueProperties(InputBase.DisplayTextProperty, Value); SyncTextAndValueProperties(NumericUpDown.DisplayTextProperty, Value);
} }
#endregion //FormatString #endregion //FormatString
@ -104,23 +103,21 @@ namespace Microsoft.Windows.Controls
protected override object ConvertTextToValue(string text) protected override object ConvertTextToValue(string text)
{ {
object result = null;
NumberFormatInfo info = NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture); NumberFormatInfo info = NumberFormatInfo.GetInstance(CultureInfo.CurrentCulture);
if (text.Contains(info.PercentSymbol))
try
{ {
if (ValueType == typeof(decimal)) result = FormatString.Contains("P") ? ParsePercent(text, ValueType, info) : ParseDataValue(text, ValueType, info);
return TryParceDecimalPercent(text, info);
else
return TryParceDoublePercent(text, info);
} }
else catch
{ {
if (ValueType == typeof(decimal)) TextBox.Text = DisplayText = ConvertValueToText(Value);
return TryParceDecimal(text, info); return Value;
else if (ValueType == typeof(int))
return TryParceInteger(text, info);
else
return TryParceDouble(text, info);
} }
return result;
} }
protected override string ConvertValueToText(object value) protected override string ConvertValueToText(object value)
@ -131,21 +128,13 @@ namespace Microsoft.Windows.Controls
protected override void OnIncrement() protected override void OnIncrement()
{ {
double newValue = (double)(Convert.ToDecimal(Value) + (decimal)Increment); double newValue = (double)(Convert.ToDecimal(Value) + (decimal)Increment);
Value = ValueType != typeof(Double) ? Convert.ChangeType(newValue, ValueType) : newValue;
if (ValueType != typeof(Double))
Value = Convert.ChangeType(newValue, ValueType);
else
Value = newValue;
} }
protected override void OnDecrement() protected override void OnDecrement()
{ {
double newValue = (double)(Convert.ToDecimal(Value) - (decimal)Increment); double newValue = (double)(Convert.ToDecimal(Value) - (decimal)Increment);
Value = ValueType != typeof(Double) ? Convert.ChangeType(newValue, ValueType) : newValue;
if (ValueType != typeof(Double))
Value = Convert.ChangeType(newValue, ValueType);
else
Value = newValue;
} }
#endregion //Base Class Overrides #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; try
text = text.Replace(info.PercentSymbol, null); {
result = TryParceDouble(text, info); if (typeof(double) == dataType)
return result / 100; {
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; return double.Parse(text, NumberStyles.Any, info);
text = text.Replace(info.PercentSymbol, null);
result = TryParceDecimal(text, info);
return result / 100;
} }
private decimal TryParceDecimal(string text, NumberFormatInfo info) private static float ParseFloat(string text, NumberFormatInfo info)
{ {
decimal result; double result = double.Parse(text, NumberStyles.Any, info);
if (!decimal.TryParse(text, NumberStyles.Any, info, out result)) return (float)result;
{
//an error occured now lets reset our value
result = Convert.ToDecimal(Value);
TextBox.Text = DisplayText = ConvertValueToText(result);
}
return result;
} }
private double TryParceDouble(string text, NumberFormatInfo info) private static decimal ParseDecimal(string text, NumberFormatInfo info)
{ {
double result; return decimal.Parse(text, NumberStyles.Any, info);
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;
} }
private int TryParceInteger(string text, NumberFormatInfo info) private static object ParseWholeNumber(string text, Type dataType, NumberFormatInfo info)
{ {
int result; decimal result = decimal.Parse(text, NumberStyles.Any, info);
if (!int.TryParse(text, NumberStyles.Any, info, out result)) return Convert.ChangeType(result, dataType, info);
{
//an error occured now lets reset our value
result = Convert.ToInt32(Value);
TextBox.Text = DisplayText = ConvertValueToText(result);
}
return result;
} }
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 #endregion //Methods
} }
} }

25
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml

@ -2,8 +2,15 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib" xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:Microsoft.Windows.Controls" 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"> xmlns:magConverters="clr-namespace:Microsoft.Windows.Controls.Mag.Converters">
<!-- =============================================================================== -->
<!-- Common Converters -->
<!-- =============================================================================== -->
<coreConverters:InverseBoolConverter x:Key="InverseBoolConverter" />
<!-- =============================================================================== --> <!-- =============================================================================== -->
<!-- Common Styles --> <!-- Common Styles -->
<!-- =============================================================================== --> <!-- =============================================================================== -->
@ -1094,6 +1101,7 @@
FontStyle="{TemplateBinding FontStyle}" FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}" FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding Foreground}" Foreground="{TemplateBinding Foreground}"
IsReadOnly="{Binding IsEditable, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
MinWidth="20" AcceptsReturn="False" MinWidth="20" AcceptsReturn="False"
TextAlignment="Right" TextWrapping="NoWrap" TextAlignment="Right" TextWrapping="NoWrap"
Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" /> Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" />
@ -1112,14 +1120,15 @@
<Setter.Value> <Setter.Value>
<ControlTemplate TargetType="{x:Type local:MaskedTextBox}"> <ControlTemplate TargetType="{x:Type local:MaskedTextBox}">
<TextBox x:Name="TextBox" <TextBox x:Name="TextBox"
FontFamily="{TemplateBinding FontFamily}" FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}" FontSize="{TemplateBinding FontSize}"
FontStretch="{TemplateBinding FontStretch}" FontStretch="{TemplateBinding FontStretch}"
FontStyle="{TemplateBinding FontStyle}" FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}" FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding Foreground}" Foreground="{TemplateBinding Foreground}"
MinWidth="20" AcceptsReturn="False" TextWrapping="NoWrap" IsReadOnly="{Binding IsEditable, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" /> MinWidth="20" AcceptsReturn="False" TextWrapping="NoWrap"
Text="{Binding DisplayText, RelativeSource={RelativeSource TemplatedParent}}" />
</ControlTemplate> </ControlTemplate>
</Setter.Value> </Setter.Value>
</Setter> </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 Microsoft.Windows.Controls.Primitives;
using System.Windows.Input; using System.Windows.Input;
namespace Microsoft.Windows.Controls.Primitives namespace Microsoft.Windows.Controls
{ {
public abstract class UpDownBase : InputBase 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\SpinEventArgs.cs" />
<Compile Include="ButtonSpinner\Spinner.cs" /> <Compile Include="ButtonSpinner\Spinner.cs" />
<Compile Include="ButtonSpinner\ValidSpinDirections.cs" /> <Compile Include="ButtonSpinner\ValidSpinDirections.cs" />
<Compile Include="Core\Converters\InverseBoolConverter.cs" />
<Compile Include="Magnifier\Converters\BorderThicknessToStrokeThicknessConverter.cs" /> <Compile Include="Magnifier\Converters\BorderThicknessToStrokeThicknessConverter.cs" />
<Compile Include="Magnifier\Converters\RadiusConverter.cs" /> <Compile Include="Magnifier\Converters\RadiusConverter.cs" />
<Compile Include="Magnifier\Magnifier.cs" /> <Compile Include="Magnifier\Magnifier.cs" />
@ -110,7 +111,7 @@
<Compile Include="RichTextBox\Formatters\RtfFormatter.cs" /> <Compile Include="RichTextBox\Formatters\RtfFormatter.cs" />
<Compile Include="RichTextBox\Formatters\XamlFormatter.cs" /> <Compile Include="RichTextBox\Formatters\XamlFormatter.cs" />
<Compile Include="RichTextBox\RichTextBox.cs" /> <Compile Include="RichTextBox\RichTextBox.cs" />
<Compile Include="Core\Primitives\UpDownBase.cs" /> <Compile Include="UpDownBase\UpDownBase.cs" />
<Compile Include="VisualStates.cs" /> <Compile Include="VisualStates.cs" />
<Compile Include="MessageBox\VisualStates.MessageBox.cs" /> <Compile Include="MessageBox\VisualStates.MessageBox.cs" />
<EmbeddedResource Include="Properties\Resources.resx"> <EmbeddedResource Include="Properties\Resources.resx">
@ -150,9 +151,7 @@
<ItemGroup> <ItemGroup>
<Resource Include="RichTextBoxFormatBar\Images\Underline16.png" /> <Resource Include="RichTextBoxFormatBar\Images\Underline16.png" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup />
<Folder Include="UpDownBase\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- 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. Other similar extension points exist, see Microsoft.Common.targets.

Loading…
Cancel
Save