From 4cc111cd095032e5d1a987dae676a0cc9fc82f79 Mon Sep 17 00:00:00 2001 From: brianlagunas_cp Date: Mon, 24 Jan 2011 21:01:43 +0000 Subject: [PATCH] DateTimeUpDown: added support for custom format. Simply specify the Format as Custom and then provide a FormatString. Above snippet will show as 13:30 --- .../DateTimeUpDown/DateTimeFormat.cs | 1 + .../DateTimeUpDown/DateTimeUpDown.cs | 32 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeFormat.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeFormat.cs index 64591695..f6c54305 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeFormat.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeFormat.cs @@ -4,6 +4,7 @@ namespace Microsoft.Windows.Controls { public enum DateTimeFormat { + Custom, FullDateTime, LongDate, LongTime, diff --git a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs index 0af73e15..73608120 100644 --- a/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs +++ b/ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs @@ -38,11 +38,39 @@ namespace Microsoft.Windows.Controls protected virtual void OnFormatChanged(DateTimeFormat oldValue, DateTimeFormat newValue) { - InitializeDateTimeInfoList(); + //if using a CustomFormat then the initialization occurs on the CustomFormatString property + if (newValue != DateTimeFormat.Custom) + InitializeDateTimeInfoList(); } #endregion //Format + #region FormatString + + public static readonly DependencyProperty FormatStringProperty = DependencyProperty.Register("FormatString", typeof(string), typeof(DateTimeUpDown), new UIPropertyMetadata(default(String), OnFormatStringChanged)); + public string FormatString + { + get { return (string)GetValue(FormatStringProperty); } + set { SetValue(FormatStringProperty, value); } + } + + private static void OnFormatStringChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) + { + DateTimeUpDown dateTimeUpDown = o as DateTimeUpDown; + if (dateTimeUpDown != null) + dateTimeUpDown.OnFormatStringChanged((string)e.OldValue, (string)e.NewValue); + } + + protected virtual void OnFormatStringChanged(string oldValue, string newValue) + { + if (string.IsNullOrEmpty(newValue)) + throw new ArgumentException("CustomFormat should be specified.", FormatString); + + InitializeDateTimeInfoList(); + } + + #endregion //FormatString + #endregion //Properties #region Constructors @@ -369,6 +397,8 @@ namespace Microsoft.Windows.Controls return DateTimeFormatInfo.UniversalSortableDateTimePattern; case DateTimeFormat.YearMonth: return DateTimeFormatInfo.YearMonthPattern; + case DateTimeFormat.Custom: + return FormatString; default: throw new ArgumentException("Not a supported format"); }