Browse Source

NumericUpDown: changed default ValueType to Decimal.

initial check in of new Calculator UpDown control.
pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
8cc7bb073b
  1. 121
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CalculatorUpDown/Implementation/CalculatorUpDown.cs
  2. 107
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CalculatorUpDown/Themes/Generic.xaml
  3. 51
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/NumericUpDown.cs
  4. 1
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Themes/Generic.xaml
  5. 5
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

121
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CalculatorUpDown/Implementation/CalculatorUpDown.cs

@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls
{
public class CalculatorUpDown : NumericUpDown
{
#region Members
private Popup _calculatorPopup;
private Calculator _calculator;
#endregion //Members
#region Properties
#region IsOpen
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register("IsOpen", typeof(bool), typeof(CalculatorUpDown), new UIPropertyMetadata(false, OnIsOpenChanged));
public bool IsOpen
{
get { return (bool)GetValue(IsOpenProperty); }
set { SetValue(IsOpenProperty, value); }
}
private static void OnIsOpenChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
CalculatorUpDown calculatorUpDown = o as CalculatorUpDown;
if (calculatorUpDown != null)
calculatorUpDown.OnIsOpenChanged((bool)e.OldValue, (bool)e.NewValue);
}
protected virtual void OnIsOpenChanged(bool oldValue, bool newValue)
{
}
#endregion //IsOpen
#endregion //Properties
#region Constructors
static CalculatorUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CalculatorUpDown), new FrameworkPropertyMetadata(typeof(CalculatorUpDown)));
}
public CalculatorUpDown()
{
Keyboard.AddKeyDownHandler(this, OnKeyDown);
Mouse.AddPreviewMouseDownOutsideCapturedElementHandler(this, OnMouseDownOutsideCapturedElement);
}
#endregion //Constructors
#region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_calculatorPopup = (Popup)GetTemplateChild("PART_CalculatorPopup");
_calculatorPopup.Opened += CalculatorPopup_Opened;
_calculator = (Calculator)GetTemplateChild("PART_Calculator");
}
#endregion //Base Class Overrides
#region Event Handlers
void CalculatorPopup_Opened(object sender, EventArgs e)
{
_calculator.Focus();
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape:
case Key.Tab:
{
CloseCalculatorUpDown();
e.Handled = true;
break;
}
}
}
private void OnMouseDownOutsideCapturedElement(object sender, MouseButtonEventArgs e)
{
CloseCalculatorUpDown();
}
#endregion //Event Handlers
#region Methods
private void CloseCalculatorUpDown()
{
if (IsOpen)
IsOpen = false;
ReleaseMouseCapture();
}
#endregion //Methods
}
}

107
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/CalculatorUpDown/Themes/Generic.xaml

@ -0,0 +1,107 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Microsoft.Windows.Controls"
xmlns:chrome="clr-namespace:Microsoft.Windows.Controls.Chromes"
xmlns:coreConverters="clr-namespace:Microsoft.Windows.Controls.Core.Converters">
<coreConverters:InverseBoolConverter x:Key="InverseBoolConverter" />
<LinearGradientBrush x:Key="PopupDarkBorderBrush" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFA3AEB9" Offset="0"/>
<GradientStop Color="#FF8399A9" Offset="0.375"/>
<GradientStop Color="#FF718597" Offset="0.375"/>
<GradientStop Color="#FF617584" Offset="1"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="PopupBackgroundBrush" StartPoint="0,0" EndPoint="0,1">
<LinearGradientBrush.GradientStops>
<GradientStopCollection>
<GradientStop Offset="0" Color="#FFffffff"/>
<GradientStop Offset="1" Color="#FFE8EBED"/>
</GradientStopCollection>
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
<Style x:Key="CalculatorToggleButtonStyle" TargetType="ToggleButton">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ToggleButton">
<Grid SnapsToDevicePixels="True">
<chrome:ButtonChrome x:Name="ToggleButtonChrome"
CornerRadius="0,2.75,2.75,0"
RenderMouseOver="{TemplateBinding IsMouseOver}"
RenderPressed="{TemplateBinding IsPressed}" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
<Grid x:Name="arrowGlyph" IsHitTestVisible="False" Grid.Column="1" Margin="5">
<Path Width="7" Height="4" Data="M 0,1 C0,1 0,0 0,0 0,0 3,0 3,0 3,0 3,1 3,1 3,1 4,1 4,1 4,1 4,0 4,0 4,0 7,0 7,0 7,0 7,1 7,1 7,1 6,1 6,1 6,1 6,2 6,2 6,2 5,2 5,2 5,2 5,3 5,3 5,3 4,3 4,3 4,3 4,4 4,4 4,4 3,4 3,4 3,4 3,3 3,3 3,3 2,3 2,3 2,3 2,2 2,2 2,2 1,2 1,2 1,2 1,1 1,1 1,1 0,1 0,1 z" Fill="#FF000000"/>
</Grid>
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- =============================================================================== -->
<!-- CalculatorUpDown -->
<!-- =============================================================================== -->
<Style TargetType="{x:Type local:CalculatorUpDown}">
<Setter Property="BorderThickness" Value="1,1,0,1" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CalculatorUpDown}">
<Grid>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<local:ButtonSpinner x:Name="Spinner" IsTabStop="False" BorderThickness="{TemplateBinding BorderThickness}"
AllowSpin="{TemplateBinding AllowSpin}" ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
<TextBox x:Name="TextBox" BorderThickness="0"
Background="{TemplateBinding Background}"
FontFamily="{TemplateBinding FontFamily}"
FontSize="{TemplateBinding FontSize}"
FontStretch="{TemplateBinding FontStretch}"
FontStyle="{TemplateBinding FontStyle}"
FontWeight="{TemplateBinding FontWeight}"
Foreground="{TemplateBinding Foreground}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
IsReadOnly="{Binding IsEditable, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"
MinWidth="20" AcceptsReturn="False"
TextWrapping="NoWrap"
TabIndex="{TemplateBinding TabIndex}"
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}" />
</local:ButtonSpinner>
<ToggleButton x:Name="_calculatorToggleButton" Grid.Column="1"
IsChecked="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource CalculatorToggleButtonStyle}"
IsHitTestVisible="{Binding IsOpen, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource InverseBoolConverter}}"/>
</Grid>
<Popup x:Name="PART_CalculatorPopup" IsOpen="{Binding IsChecked, ElementName=_calculatorToggleButton}" StaysOpen="False">
<Border BorderThickness="1" Background="{StaticResource PopupBackgroundBrush}" BorderBrush="{StaticResource PopupDarkBorderBrush}" Padding="3">
<StackPanel>
<local:Calculator x:Name="PART_Calculator" Value="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" BorderThickness="0" />
</StackPanel>
</Border>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

51
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/NumericUpDown.cs

@ -12,10 +12,10 @@ namespace Microsoft.Windows.Controls
#region Minimum
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(Double.MinValue, OnMinimumPropertyChanged));
public double Minimum
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(Decimal.MinValue, OnMinimumPropertyChanged));
public decimal Minimum
{
get { return (double)GetValue(MinimumProperty); }
get { return (decimal)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
@ -29,10 +29,10 @@ namespace Microsoft.Windows.Controls
#region Maximum
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(NumericUpDown), new PropertyMetadata(Double.MaxValue, OnMaximumPropertyChanged));
public double Maximum
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(Decimal.MaxValue, OnMaximumPropertyChanged));
public decimal Maximum
{
get { return (double)GetValue(MaximumProperty); }
get { return (decimal)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
@ -46,10 +46,10 @@ namespace Microsoft.Windows.Controls
#region Increment
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(NumericUpDown), new PropertyMetadata(1.0));
public double Increment
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(decimal), typeof(NumericUpDown), new PropertyMetadata(1M));
public decimal Increment
{
get { return (double)GetValue(IncrementProperty); }
get { return (decimal)GetValue(IncrementProperty); }
set { SetValue(IncrementProperty, value); }
}
@ -95,8 +95,8 @@ namespace Microsoft.Windows.Controls
static NumericUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(NumericUpDown)));
ValueTypeProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(double)));
ValueProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(default(Double)));
ValueTypeProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(typeof(decimal)));
ValueProperty.OverrideMetadata(typeof(NumericUpDown), new FrameworkPropertyMetadata(default(decimal)));
}
#endregion //Constructors
@ -140,7 +140,7 @@ namespace Microsoft.Windows.Controls
{
if (value == null) return value;
double val = Convert.ToDouble(value);
decimal val = Convert.ToDecimal(value);
if (val < Minimum)
{
@ -190,19 +190,19 @@ namespace Microsoft.Windows.Controls
return "Negative-Infinity";
}
return (Convert.ToDouble(Value)).ToString(FormatString, CultureInfo.CurrentCulture);
return (Convert.ToDecimal(Value)).ToString(FormatString, CultureInfo.CurrentCulture);
}
protected override void OnIncrement()
{
double newValue = (double)(Convert.ToDouble(Value) + (double)Increment);
Value = ValueType != typeof(Double) ? Convert.ChangeType(newValue, ValueType) : newValue;
decimal newValue = (Convert.ToDecimal(Value) + Increment);
Value = ValueType != typeof(Decimal) ? Convert.ChangeType(newValue, ValueType) : newValue;
}
protected override void OnDecrement()
{
double newValue = (double)(Convert.ToDouble(Value) - (double)Increment);
Value = ValueType != typeof(Double) ? Convert.ChangeType(newValue, ValueType) : newValue;
decimal newValue = (Convert.ToDecimal(Value) - Increment);
Value = ValueType != typeof(Decimal) ? Convert.ChangeType(newValue, ValueType) : newValue;
}
#endregion //Base Class Overrides
@ -234,12 +234,12 @@ namespace Microsoft.Windows.Controls
{
ValidSpinDirections validDirections = ValidSpinDirections.None;
if (Convert.ToDouble(Value) < Maximum)
if (Convert.ToDecimal(Value) < Maximum)
{
validDirections = validDirections | ValidSpinDirections.Increase;
}
if (Convert.ToDouble(Value) > Minimum)
if (Convert.ToDecimal(Value) > Minimum)
{
validDirections = validDirections | ValidSpinDirections.Decrease;
}
@ -256,7 +256,11 @@ namespace Microsoft.Windows.Controls
{
try
{
if (typeof(double) == dataType)
if (typeof(decimal) == dataType)
{
return ParseDecimal(text, info);
}
else if (typeof(double) == dataType)
{
return ParseDouble(text, info);
}
@ -271,10 +275,6 @@ namespace Microsoft.Windows.Controls
{
return ParseWholeNumber(text, dataType, info);
}
else if (typeof(decimal) == dataType)
{
return ParseDecimal(text, info);
}
else
{
throw new ArgumentException("Type not supported");
@ -293,8 +293,7 @@ namespace Microsoft.Windows.Controls
private static float ParseFloat(string text, NumberFormatInfo info)
{
double result = double.Parse(text, NumberStyles.Any, info);
return (float)result;
return float.Parse(text, NumberStyles.Any, info);
}
private static decimal ParseDecimal(string text, NumberFormatInfo info)

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

@ -5,6 +5,7 @@
<ResourceDictionary Source="/WPFToolkit.Extended;component/BusyIndicator/Themes/Generic.xaml" />
<ResourceDictionary Source="/WPFToolkit.Extended;component/ButtonSpinner/Themes/Generic.xaml" />
<ResourceDictionary Source="/WPFToolkit.Extended;component/Calculator/Themes/Generic.xaml" />
<ResourceDictionary Source="/WPFToolkit.Extended;component/CalculatorUpDown/Themes/Generic.xaml" />
<ResourceDictionary Source="/WPFToolkit.Extended;component/ChildWindow/Themes/Generic.xaml" />
<ResourceDictionary Source="/WPFToolkit.Extended;component/ColorCanvas/Themes/Generic.xaml" />
<ResourceDictionary Source="/WPFToolkit.Extended;component/ColorPicker/Themes/Generic.xaml" />

5
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -67,6 +67,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="CalculatorUpDown\Themes\Generic.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="Calculator\Themes\Generic.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@ -143,6 +147,7 @@
<ItemGroup>
<Compile Include="BusyIndicator\Implementation\BusyIndicator.cs" />
<Compile Include="ButtonSpinner\Implementation\ButtonSpinner.cs" />
<Compile Include="CalculatorUpDown\Implementation\CalculatorUpDown.cs" />
<Compile Include="Calculator\Implementation\Calculator.cs" />
<Compile Include="Calculator\Implementation\CalculatorCommands.cs" />
<Compile Include="ChildWindow\Implementation\ChildWindow.cs" />

Loading…
Cancel
Save