Browse Source

check in of new DecimalUpDown and DoubleUpDown. All numeric UpDown controls shoudl be functional. Now it is time to refactor.

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
51e6a10b9f
  1. 12
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/InputBase.cs
  2. 42
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/UpDownBase.cs
  3. 235
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/DecimalUpDown.cs
  4. 235
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/DoubleUpDown.cs
  5. 161
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/IntegerUpDown.cs
  6. 1
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/NumericUpDown.cs
  7. 76
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Themes/Generic.xaml
  8. 2
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

12
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/InputBase.cs

@ -1,6 +1,7 @@
using System; using System;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows; using System.Windows;
using System.Globalization;
namespace Microsoft.Windows.Controls.Primitives namespace Microsoft.Windows.Controls.Primitives
{ {
@ -8,6 +9,17 @@ namespace Microsoft.Windows.Controls.Primitives
{ {
#region Properties #region Properties
#region CultureInfo
public static readonly DependencyProperty CultureInfoProperty = DependencyProperty.Register("CultureInfo", typeof(CultureInfo), typeof(InputBase), new UIPropertyMetadata(CultureInfo.CurrentCulture));
public CultureInfo CultureInfo
{
get { return (CultureInfo)GetValue(CultureInfoProperty); }
set { SetValue(CultureInfoProperty, value); }
}
#endregion //CultureInfo
#region IsEditable #region IsEditable
public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(InputBase), new PropertyMetadata(true)); public static readonly DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(InputBase), new PropertyMetadata(true));

42
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/UpDownBase.cs

@ -3,6 +3,7 @@ using System.Windows.Controls;
using Microsoft.Windows.Controls.Primitives; using Microsoft.Windows.Controls.Primitives;
using System.Windows.Input; using System.Windows.Input;
using System.Windows; using System.Windows;
using System.Windows.Data;
namespace Microsoft.Windows.Controls.Primitives namespace Microsoft.Windows.Controls.Primitives
{ {
@ -187,6 +188,13 @@ namespace Microsoft.Windows.Controls.Primitives
#endregion //Methods #endregion //Methods
} }
/// <summary> /// <summary>
/// This is going to be the new UpDownBase class. In order to reliably support the different numeric data types, I need to create a specific control for each data type. /// This is going to be the new UpDownBase class. In order to reliably support the different numeric data types, I need to create a specific control for each data type.
/// So instead of copying and pasting I am going to try to create a generic base class that has most of the logic in it. This will make the NumericUpDown control obsolete /// So instead of copying and pasting I am going to try to create a generic base class that has most of the logic in it. This will make the NumericUpDown control obsolete
@ -216,8 +224,8 @@ namespace Microsoft.Windows.Controls.Primitives
#region Properties #region Properties
protected Spinner Spinner { get; private set; }
protected TextBox TextBox { get; private set; } protected TextBox TextBox { get; private set; }
internal Spinner Spinner { get; private set; }
#region AllowSpin #region AllowSpin
@ -260,7 +268,7 @@ namespace Microsoft.Windows.Controls.Primitives
} }
protected virtual T OnCoerceValue(T value) protected virtual T OnCoerceValue(T value)
{ {
return value; return value;
} }
@ -273,7 +281,7 @@ namespace Microsoft.Windows.Controls.Primitives
protected virtual void OnValueChanged(T oldValue, T newValue) protected virtual void OnValueChanged(T oldValue, T newValue)
{ {
SyncTextAndValueProperties(NumericUpDown.ValueProperty, newValue); SyncTextAndValueProperties(NumericUpDown.ValueProperty, string.Empty);
RoutedPropertyChangedEventArgs<T> args = new RoutedPropertyChangedEventArgs<T>(oldValue, newValue); RoutedPropertyChangedEventArgs<T> args = new RoutedPropertyChangedEventArgs<T>(oldValue, newValue);
args.RoutedEvent = NumericUpDown.ValueChangedEvent; args.RoutedEvent = NumericUpDown.ValueChangedEvent;
@ -292,6 +300,14 @@ namespace Microsoft.Windows.Controls.Primitives
#region Base Class Overrides #region Base Class Overrides
protected override void OnAccessKey(AccessKeyEventArgs e)
{
if (TextBox != null)
TextBox.Focus();
base.OnAccessKey(e);
}
public override void OnApplyTemplate() public override void OnApplyTemplate()
{ {
base.OnApplyTemplate(); base.OnApplyTemplate();
@ -313,14 +329,14 @@ namespace Microsoft.Windows.Controls.Primitives
{ {
case Key.Up: case Key.Up:
{ {
if (AllowSpin) if (AllowSpin && IsEditable)
DoIncrement(); DoIncrement();
e.Handled = true; e.Handled = true;
break; break;
} }
case Key.Down: case Key.Down:
{ {
if (AllowSpin) if (AllowSpin && IsEditable)
DoDecrement(); DoDecrement();
e.Handled = true; e.Handled = true;
break; break;
@ -328,7 +344,10 @@ namespace Microsoft.Windows.Controls.Primitives
case Key.Enter: case Key.Enter:
{ {
if (IsEditable) if (IsEditable)
SyncTextAndValueProperties(UpDownBase.TextProperty, TextBox.Text); {
var binding = BindingOperations.GetBindingExpression(TextBox, System.Windows.Controls.TextBox.TextProperty);
binding.UpdateSource();
}
break; break;
} }
} }
@ -338,7 +357,7 @@ namespace Microsoft.Windows.Controls.Primitives
{ {
base.OnMouseWheel(e); base.OnMouseWheel(e);
if (!e.Handled && AllowSpin) if (!e.Handled && AllowSpin && IsEditable)
{ {
if (e.Delta < 0) if (e.Delta < 0)
{ {
@ -416,7 +435,7 @@ namespace Microsoft.Windows.Controls.Primitives
} }
} }
private void SyncTextAndValueProperties(DependencyProperty p, object newValue) private void SyncTextAndValueProperties(DependencyProperty p, string text)
{ {
//prevents recursive syncing properties //prevents recursive syncing properties
if (_isSyncingTextAndValueProperties) if (_isSyncingTextAndValueProperties)
@ -427,11 +446,10 @@ namespace Microsoft.Windows.Controls.Primitives
//this only occures when the user typed in the value //this only occures when the user typed in the value
if (InputBase.TextProperty == p) if (InputBase.TextProperty == p)
{ {
string text = newValue == null ? String.Empty : newValue.ToString(); Value = ConvertTextToValue(text);
SetValue(UpDownBase<T>.ValueProperty, ConvertTextToValue(text));
} }
SetValue(InputBase.TextProperty, ConvertValueToText(newValue)); Text = ConvertValueToText();
_isSyncingTextAndValueProperties = false; _isSyncingTextAndValueProperties = false;
} }
@ -450,7 +468,7 @@ namespace Microsoft.Windows.Controls.Primitives
protected abstract T ConvertTextToValue(string text); protected abstract T ConvertTextToValue(string text);
protected abstract string ConvertValueToText(object value); protected abstract string ConvertValueToText();
#endregion //Abstract #endregion //Abstract

235
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/DecimalUpDown.cs

@ -0,0 +1,235 @@
using System;
using Microsoft.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;
namespace Microsoft.Windows.Controls
{
public class DecimalUpDown : UpDownBase<decimal?>
{
#region Properties
#region DefaultValue
//can possibly be in base class
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(decimal), typeof(DecimalUpDown), new UIPropertyMetadata(default(decimal)));
public decimal DefaultValue
{
get { return (decimal)GetValue(DefaultValueProperty); }
set { SetValue(DefaultValueProperty, value); }
}
#endregion //DefaultValue
#region FormatString
public static readonly DependencyProperty FormatStringProperty = DependencyProperty.Register("FormatString", typeof(string), typeof(DecimalUpDown), new UIPropertyMetadata(String.Empty));
public string FormatString
{
get { return (string)GetValue(FormatStringProperty); }
set { SetValue(FormatStringProperty, value); }
}
#endregion //FormatString
#region Increment
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(decimal), typeof(DecimalUpDown), new PropertyMetadata(1m));
public decimal Increment
{
get { return (decimal)GetValue(IncrementProperty); }
set { SetValue(IncrementProperty, value); }
}
#endregion
#region Maximum
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(decimal), typeof(DecimalUpDown), new UIPropertyMetadata(decimal.MaxValue, OnMaximumChanged));
public decimal Maximum
{
get { return (decimal)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
private static void OnMaximumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
DecimalUpDown decimalUpDown = o as DecimalUpDown;
if (decimalUpDown != null)
decimalUpDown.OnMaximumChanged((decimal)e.OldValue, (decimal)e.NewValue);
}
protected virtual void OnMaximumChanged(decimal oldValue, decimal newValue)
{
//SetValidSpinDirection();
}
#endregion //Maximum
#region Minimum
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(decimal), typeof(DecimalUpDown), new UIPropertyMetadata(decimal.MinValue, OnMinimumChanged));
public decimal Minimum
{
get { return (decimal)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
private static void OnMinimumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
DecimalUpDown decimalUpDown = o as DecimalUpDown;
if (decimalUpDown != null)
decimalUpDown.OnMinimumChanged((decimal)e.OldValue, (decimal)e.NewValue);
}
protected virtual void OnMinimumChanged(decimal oldValue, decimal newValue)
{
//SetValidSpinDirection();
}
#endregion //Minimum
#region SelectAllOnGotFocus
public static readonly DependencyProperty SelectAllOnGotFocusProperty = DependencyProperty.Register("SelectAllOnGotFocus", typeof(bool), typeof(DecimalUpDown), new PropertyMetadata(false));
public bool SelectAllOnGotFocus
{
get { return (bool)GetValue(SelectAllOnGotFocusProperty); }
set { SetValue(SelectAllOnGotFocusProperty, value); }
}
#endregion //SelectAllOnGotFocus
#endregion //Properties
#region Constructors
static DecimalUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DecimalUpDown), new FrameworkPropertyMetadata(typeof(DecimalUpDown)));
}
#endregion //Constructors
#region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (SelectAllOnGotFocus)
{
//in order to select all the text we must handle both the keybord (tabbing) and mouse (clicking) events
TextBox.GotKeyboardFocus += OnTextBoxGotKeyBoardFocus;
TextBox.PreviewMouseLeftButtonDown += OnTextBoxPreviewMouseLeftButtonDown;
}
//SetValidSpinDirection();
}
protected override decimal? OnCoerceValue(decimal? value)
{
if (value == null) return value;
decimal val = value.Value;
if (value < Minimum)
return Minimum;
else if (value > Maximum)
return Maximum;
else
return value;
}
protected override void OnIncrement()
{
if (Value.HasValue)
Value += Increment;
else
Value = DefaultValue;
}
protected override void OnDecrement()
{
if (Value.HasValue)
Value -= Increment;
else
Value = DefaultValue;
}
protected override decimal? ConvertTextToValue(string text)
{
decimal? result = null;
if (String.IsNullOrEmpty(text))
return result;
try
{
result = Decimal.Parse(text, System.Globalization.NumberStyles.Any, CultureInfo);
}
catch
{
Text = ConvertValueToText();
return Value;
}
return result;
}
protected override string ConvertValueToText()
{
if (Value == null)
return string.Empty;
return Value.Value.ToString(FormatString, CultureInfo);
}
protected override void OnValueChanged(decimal? oldValue, decimal? newValue)
{
//SetValidSpinDirection();
base.OnValueChanged(oldValue, newValue);
}
#endregion //Base Class Overrides
#region Event Handlers
private void OnTextBoxGotKeyBoardFocus(object sender, RoutedEventArgs e)
{
TextBox.SelectAll();
}
void OnTextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!TextBox.IsKeyboardFocused)
{
e.Handled = true;
TextBox.Focus();
}
}
#endregion //Event Handlers
#region Methods
/// <summary>
/// Sets the valid spin direction based on current value, minimum and maximum.
/// </summary>
//private void SetValidSpinDirection()
//{
// ValidSpinDirections validDirections = ValidSpinDirections.None;
// if (Value < Maximum)
// validDirections = validDirections | ValidSpinDirections.Increase;
// if (Value > Minimum)
// validDirections = validDirections | ValidSpinDirections.Decrease;
// if (Spinner != null)
// Spinner.ValidSpinDirection = validDirections;
//}
#endregion //Methods
}
}

235
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/DoubleUpDown.cs

@ -0,0 +1,235 @@
using System;
using Microsoft.Windows.Controls.Primitives;
using System.Windows;
using System.Windows.Input;
namespace Microsoft.Windows.Controls
{
public class DoubleUpDown : UpDownBase<double?>
{
#region Properties
#region DefaultValue
//can possibly be in base class
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(double), typeof(DoubleUpDown), new UIPropertyMetadata(default(double)));
public double DefaultValue
{
get { return (double)GetValue(DefaultValueProperty); }
set { SetValue(DefaultValueProperty, value); }
}
#endregion //DefaultValue
#region FormatString
public static readonly DependencyProperty FormatStringProperty = DependencyProperty.Register("FormatString", typeof(string), typeof(DoubleUpDown), new UIPropertyMetadata(String.Empty));
public string FormatString
{
get { return (string)GetValue(FormatStringProperty); }
set { SetValue(FormatStringProperty, value); }
}
#endregion //FormatString
#region Increment
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(DoubleUpDown), new PropertyMetadata(1d));
public double Increment
{
get { return (double)GetValue(IncrementProperty); }
set { SetValue(IncrementProperty, value); }
}
#endregion
#region Maximum
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(double), typeof(DoubleUpDown), new UIPropertyMetadata(double.MaxValue, OnMaximumChanged));
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
private static void OnMaximumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
DoubleUpDown doubleUpDown = o as DoubleUpDown;
if (doubleUpDown != null)
doubleUpDown.OnMaximumChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnMaximumChanged(double oldValue, double newValue)
{
//SetValidSpinDirection();
}
#endregion //Maximum
#region Minimum
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(double), typeof(DoubleUpDown), new UIPropertyMetadata(double.MinValue, OnMinimumChanged));
public double Minimum
{
get { return (double)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
private static void OnMinimumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
DoubleUpDown doubleUpDown = o as DoubleUpDown;
if (doubleUpDown != null)
doubleUpDown.OnMinimumChanged((double)e.OldValue, (double)e.NewValue);
}
protected virtual void OnMinimumChanged(double oldValue, double newValue)
{
//SetValidSpinDirection();
}
#endregion //Minimum
#region SelectAllOnGotFocus
public static readonly DependencyProperty SelectAllOnGotFocusProperty = DependencyProperty.Register("SelectAllOnGotFocus", typeof(bool), typeof(DoubleUpDown), new PropertyMetadata(false));
public bool SelectAllOnGotFocus
{
get { return (bool)GetValue(SelectAllOnGotFocusProperty); }
set { SetValue(SelectAllOnGotFocusProperty, value); }
}
#endregion //SelectAllOnGotFocus
#endregion //Properties
#region Constructors
static DoubleUpDown()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DoubleUpDown), new FrameworkPropertyMetadata(typeof(DoubleUpDown)));
}
#endregion //Constructors
#region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (SelectAllOnGotFocus)
{
//in order to select all the text we must handle both the keybord (tabbing) and mouse (clicking) events
TextBox.GotKeyboardFocus += OnTextBoxGotKeyBoardFocus;
TextBox.PreviewMouseLeftButtonDown += OnTextBoxPreviewMouseLeftButtonDown;
}
//SetValidSpinDirection();
}
protected override double? OnCoerceValue(double? value)
{
if (value == null) return value;
double val = value.Value;
if (value < Minimum)
return Minimum;
else if (value > Maximum)
return Maximum;
else
return value;
}
protected override void OnIncrement()
{
if (Value.HasValue)
Value += Increment;
else
Value = DefaultValue;
}
protected override void OnDecrement()
{
if (Value.HasValue)
Value -= Increment;
else
Value = DefaultValue;
}
protected override double? ConvertTextToValue(string text)
{
double? result = null;
if (String.IsNullOrEmpty(text))
return result;
try
{
result = Double.Parse(text, System.Globalization.NumberStyles.Any, CultureInfo);
}
catch
{
Text = ConvertValueToText();
return Value;
}
return result;
}
protected override string ConvertValueToText()
{
if (Value == null)
return string.Empty;
return Value.Value.ToString(FormatString, CultureInfo);
}
protected override void OnValueChanged(double? oldValue, double? newValue)
{
//SetValidSpinDirection();
base.OnValueChanged(oldValue, newValue);
}
#endregion //Base Class Overrides
#region Event Handlers
private void OnTextBoxGotKeyBoardFocus(object sender, RoutedEventArgs e)
{
TextBox.SelectAll();
}
void OnTextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!TextBox.IsKeyboardFocused)
{
e.Handled = true;
TextBox.Focus();
}
}
#endregion //Event Handlers
#region Methods
/// <summary>
/// Sets the valid spin direction based on current value, minimum and maximum.
/// </summary>
//private void SetValidSpinDirection()
//{
// ValidSpinDirections validDirections = ValidSpinDirections.None;
// if (Value < Maximum)
// validDirections = validDirections | ValidSpinDirections.Increase;
// if (Value > Minimum)
// validDirections = validDirections | ValidSpinDirections.Decrease;
// if (Spinner != null)
// Spinner.ValidSpinDirection = validDirections;
//}
#endregion //Methods
}
}

161
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Implementation/IntegerUpDown.cs

@ -1,16 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input; using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Windows.Controls.Primitives; using Microsoft.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls namespace Microsoft.Windows.Controls
@ -31,6 +21,17 @@ namespace Microsoft.Windows.Controls
#endregion //DefaultValue #endregion //DefaultValue
#region FormatString
public static readonly DependencyProperty FormatStringProperty = DependencyProperty.Register("FormatString", typeof(string), typeof(IntegerUpDown), new UIPropertyMetadata(String.Empty));
public string FormatString
{
get { return (string)GetValue(FormatStringProperty); }
set { SetValue(FormatStringProperty, value); }
}
#endregion //FormatString
#region Increment #region Increment
public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(int), typeof(IntegerUpDown), new PropertyMetadata(1)); public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(int), typeof(IntegerUpDown), new PropertyMetadata(1));
@ -42,6 +43,63 @@ namespace Microsoft.Windows.Controls
#endregion #endregion
#region Maximum
public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(int), typeof(IntegerUpDown), new UIPropertyMetadata(int.MaxValue, OnMaximumChanged));
public int Maximum
{
get { return (int)GetValue(MaximumProperty); }
set { SetValue(MaximumProperty, value); }
}
private static void OnMaximumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
IntegerUpDown integerUpDown = o as IntegerUpDown;
if (integerUpDown != null)
integerUpDown.OnMaximumChanged((int)e.OldValue, (int)e.NewValue);
}
protected virtual void OnMaximumChanged(int oldValue, int newValue)
{
//SetValidSpinDirection();
}
#endregion //Maximum
#region Minimum
public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(int), typeof(IntegerUpDown), new UIPropertyMetadata(int.MinValue, OnMinimumChanged));
public int Minimum
{
get { return (int)GetValue(MinimumProperty); }
set { SetValue(MinimumProperty, value); }
}
private static void OnMinimumChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
IntegerUpDown integerUpDown = o as IntegerUpDown;
if (integerUpDown != null)
integerUpDown.OnMinimumChanged((int)e.OldValue, (int)e.NewValue);
}
protected virtual void OnMinimumChanged(int oldValue, int newValue)
{
//SetValidSpinDirection();
}
#endregion //Minimum
#region SelectAllOnGotFocus
public static readonly DependencyProperty SelectAllOnGotFocusProperty = DependencyProperty.Register("SelectAllOnGotFocus", typeof(bool), typeof(IntegerUpDown), new PropertyMetadata(false));
public bool SelectAllOnGotFocus
{
get { return (bool)GetValue(SelectAllOnGotFocusProperty); }
set { SetValue(SelectAllOnGotFocusProperty, value); }
}
#endregion //SelectAllOnGotFocus
#endregion //Properties #endregion //Properties
#region Constructors #region Constructors
@ -55,6 +113,34 @@ namespace Microsoft.Windows.Controls
#region Base Class Overrides #region Base Class Overrides
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
if (SelectAllOnGotFocus)
{
//in order to select all the text we must handle both the keybord (tabbing) and mouse (clicking) events
TextBox.GotKeyboardFocus += OnTextBoxGotKeyBoardFocus;
TextBox.PreviewMouseLeftButtonDown += OnTextBoxPreviewMouseLeftButtonDown;
}
//SetValidSpinDirection();
}
protected override int? OnCoerceValue(int? value)
{
if (value == null) return value;
int val = value.Value;
if (value < Minimum)
return Minimum;
else if (value > Maximum)
return Maximum;
else
return value;
}
protected override void OnIncrement() protected override void OnIncrement()
{ {
if (Value.HasValue) if (Value.HasValue)
@ -80,25 +166,70 @@ namespace Microsoft.Windows.Controls
try try
{ {
result = Int16.Parse(text, System.Globalization.NumberStyles.Any); result = Int32.Parse(text, System.Globalization.NumberStyles.Any, CultureInfo);
} }
catch catch
{ {
Text = ConvertValueToText(Value); Text = ConvertValueToText();
return Value; return Value;
} }
return result; return result;
} }
protected override string ConvertValueToText(object value) protected override string ConvertValueToText()
{ {
if (value == null) if (Value == null)
return string.Empty; return string.Empty;
return value.ToString(); return Value.Value.ToString(FormatString, CultureInfo);
}
protected override void OnValueChanged(int? oldValue, int? newValue)
{
//SetValidSpinDirection();
base.OnValueChanged(oldValue, newValue);
} }
#endregion //Base Class Overrides #endregion //Base Class Overrides
#region Event Handlers
private void OnTextBoxGotKeyBoardFocus(object sender, RoutedEventArgs e)
{
TextBox.SelectAll();
}
void OnTextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!TextBox.IsKeyboardFocused)
{
e.Handled = true;
TextBox.Focus();
}
}
#endregion //Event Handlers
#region Methods
/// <summary>
/// Sets the valid spin direction based on current value, minimum and maximum.
/// </summary>
//private void SetValidSpinDirection()
//{
// ValidSpinDirections validDirections = ValidSpinDirections.None;
// if (Value < Maximum)
// validDirections = validDirections | ValidSpinDirections.Increase;
// if (Value > Minimum)
// validDirections = validDirections | ValidSpinDirections.Decrease;
// if (Spinner != null)
// Spinner.ValidSpinDirection = validDirections;
//}
#endregion //Methods
} }
} }

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

@ -6,6 +6,7 @@ using Microsoft.Windows.Controls.Primitives;
namespace Microsoft.Windows.Controls namespace Microsoft.Windows.Controls
{ {
[Obsolete("This control has been replaced with numeric data type specific controls; DecimalUpDown, DoubleUpDown, IntegerUpDown")]
public class NumericUpDown : UpDownBase public class NumericUpDown : UpDownBase
{ {
#region Members #region Members

76
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/NumericUpDown/Themes/Generic.xaml

@ -10,6 +10,82 @@
<ContentControl Content="{Binding}" Foreground="Gray" Focusable="False" /> <ContentControl Content="{Binding}" Foreground="Gray" Focusable="False" />
</DataTemplate> </DataTemplate>
<!-- =============================================================================== -->
<!-- DecimalUpDown -->
<!-- =============================================================================== -->
<Style TargetType="{x:Type local:DecimalUpDown}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="WatermarkTemplate" Value="{StaticResource DefaultWatermarkTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DecimalUpDown}">
<local:ButtonSpinner x:Name="Spinner" IsTabStop="False" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}"
AllowSpin="{TemplateBinding AllowSpin}" ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
<local:WatermarkTextBox 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}}"
Watermark="{TemplateBinding Watermark}"
WatermarkTemplate="{TemplateBinding WatermarkTemplate}"/>
</local:ButtonSpinner>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- =============================================================================== -->
<!-- DoubleUpDown -->
<!-- =============================================================================== -->
<Style TargetType="{x:Type local:DoubleUpDown}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Focusable" Value="False" />
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
<Setter Property="HorizontalContentAlignment" Value="Right" />
<Setter Property="WatermarkTemplate" Value="{StaticResource DefaultWatermarkTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DoubleUpDown}">
<local:ButtonSpinner x:Name="Spinner" IsTabStop="False" Background="{TemplateBinding Background}" BorderThickness="{TemplateBinding BorderThickness}"
AllowSpin="{TemplateBinding AllowSpin}" ShowButtonSpinner="{TemplateBinding ShowButtonSpinner}">
<local:WatermarkTextBox 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}}"
Watermark="{TemplateBinding Watermark}"
WatermarkTemplate="{TemplateBinding WatermarkTemplate}"/>
</local:ButtonSpinner>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- =============================================================================== --> <!-- =============================================================================== -->
<!-- IntegerUpDown --> <!-- IntegerUpDown -->
<!-- =============================================================================== --> <!-- =============================================================================== -->

2
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/WPFToolkit.Extended.csproj

@ -177,6 +177,8 @@
<Compile Include="DateTimeUpDown\Implementation\DateTimePart.cs" /> <Compile Include="DateTimeUpDown\Implementation\DateTimePart.cs" />
<Compile Include="DateTimeUpDown\Implementation\DateTimeUpDown.cs" /> <Compile Include="DateTimeUpDown\Implementation\DateTimeUpDown.cs" />
<Compile Include="DateTimeUpDown\Implementation\DateTimeParser.cs" /> <Compile Include="DateTimeUpDown\Implementation\DateTimeParser.cs" />
<Compile Include="NumericUpDown\Implementation\DecimalUpDown.cs" />
<Compile Include="NumericUpDown\Implementation\DoubleUpDown.cs" />
<Compile Include="NumericUpDown\Implementation\IntegerUpDown.cs" /> <Compile Include="NumericUpDown\Implementation\IntegerUpDown.cs" />
<Compile Include="Magnifier\Implementation\Converters\BorderThicknessToStrokeThicknessConverter.cs" /> <Compile Include="Magnifier\Implementation\Converters\BorderThicknessToStrokeThicknessConverter.cs" />
<Compile Include="Magnifier\Implementation\Converters\RadiusConverter.cs" /> <Compile Include="Magnifier\Implementation\Converters\RadiusConverter.cs" />

Loading…
Cancel
Save