From 7fbd1652b58ab7cdafa74c03dbcbfd416054258a Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Tue, 15 Feb 2011 16:10:40 +0000 Subject: [PATCH] DateTimeUpDown: implemented feature request to support null DateTime values. Also implemented ability to set Value to null by simply pressing the Delete key. --- .../DateTimeUpDown/DateTimeUpDown.cs | 36 +++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs index 73608120..ff97630c 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs @@ -99,8 +99,22 @@ namespace Microsoft.Windows.Controls protected override void OnPreviewKeyDown(KeyEventArgs e) { - if (e.Key == Key.Enter) - return; + switch (e.Key) + { + case Key.Enter: + { + return; + } + case Key.Delete: + { + Value = null; + break; + } + default: + { + break; + } + } base.OnPreviewKeyDown(e); } @@ -108,7 +122,9 @@ namespace Microsoft.Windows.Controls protected override void OnValueChanged(object oldValue, object newValue) { //whenever the value changes we need to parse out the value into out DateTimeInfo segments so we can keep track of the individual pieces - ParseValueIntoDateTimeInfo(); + //but only if it is not null + if (newValue != null) + ParseValueIntoDateTimeInfo(); base.OnValueChanged(oldValue, newValue); } @@ -144,12 +160,14 @@ namespace Microsoft.Windows.Controls protected override void OnIncrement() { - UpdateDateTime(1); + if (Value != null) + UpdateDateTime(1); } protected override void OnDecrement() { - UpdateDateTime(-1); + if (Value != null) + UpdateDateTime(-1); } protected override object ConvertTextToValue(string text) @@ -161,12 +179,8 @@ namespace Microsoft.Windows.Controls { if (value == null) return string.Empty; - DateTime? dt = DateTime.Parse(value.ToString(), CultureInfo.CurrentCulture); - - if (dt.HasValue) - return dt.Value.ToString(GetFormatString(Format), CultureInfo.CurrentCulture); - else - return string.Empty; + DateTime dt = DateTime.Parse(value.ToString(), CultureInfo.CurrentCulture); + return dt.ToString(GetFormatString(Format), CultureInfo.CurrentCulture); } #endregion //Abstract