Browse Source

DateTimeUpDown: fixed bug with CultureInfo

pull/1645/head
brianlagunas_cp 15 years ago
parent
commit
cd5b6355a9
  1. 14
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/Core/Primitives/InputBase.cs
  2. 75
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/Implementation/DateTimeParser.cs
  3. 48
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/Implementation/DateTimeUpDown.cs

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

@ -11,13 +11,25 @@ namespace Microsoft.Windows.Controls.Primitives
#region CultureInfo #region CultureInfo
public static readonly DependencyProperty CultureInfoProperty = DependencyProperty.Register("CultureInfo", typeof(CultureInfo), typeof(InputBase), new UIPropertyMetadata(CultureInfo.CurrentCulture)); public static readonly DependencyProperty CultureInfoProperty = DependencyProperty.Register("CultureInfo", typeof(CultureInfo), typeof(InputBase), new UIPropertyMetadata(CultureInfo.CurrentCulture, OnCultureInfoChanged));
public CultureInfo CultureInfo public CultureInfo CultureInfo
{ {
get { return (CultureInfo)GetValue(CultureInfoProperty); } get { return (CultureInfo)GetValue(CultureInfoProperty); }
set { SetValue(CultureInfoProperty, value); } set { SetValue(CultureInfoProperty, value); }
} }
private static void OnCultureInfoChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
InputBase inputBase = o as InputBase;
if (inputBase != null)
inputBase.OnCultureInfoChanged((CultureInfo)e.OldValue, (CultureInfo)e.NewValue);
}
protected virtual void OnCultureInfoChanged(CultureInfo oldValue, CultureInfo newValue)
{
}
#endregion //CultureInfo #endregion //CultureInfo
#region IsReadOnly #region IsReadOnly

75
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/Implementation/DateTimeParser.cs

@ -1,51 +1,24 @@
using System; using System;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Globalization;
using System.Linq;
namespace Microsoft.Windows.Controls namespace Microsoft.Windows.Controls
{ {
internal class DateTimeParser internal class DateTimeParser
{ {
#region Properties public static bool TryParse(string value, string format, DateTime currentDate, CultureInfo cultureInfo, out DateTime result)
private DateTimeFormatInfo DateTimeFormatInfo { get; set; }
public string Format { get; set; }
#endregion //Properties
#region Constructors
public DateTimeParser(DateTimeFormatInfo dateTimeFormatInfo)
{
DateTimeFormatInfo = dateTimeFormatInfo;
}
public DateTimeParser(DateTimeFormatInfo dateTimeFormatInfo, string format)
{
DateTimeFormatInfo = dateTimeFormatInfo;
Format = format;
}
#endregion //Constructors
#region Methods
public bool TryParse(string value, out DateTime result, DateTime currentDate)
{ {
bool success = false; bool success = false;
result = currentDate; result = currentDate;
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(Format)) if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(format))
return false; return false;
var dateTimeString = ResolveDateTimeString(value, currentDate); var dateTimeString = ResolveDateTimeString(value, format, currentDate, cultureInfo).Trim();
if (!String.IsNullOrEmpty(dateTimeString)) if (!String.IsNullOrEmpty(dateTimeString))
success = DateTime.TryParse(dateTimeString, DateTimeFormatInfo, DateTimeStyles.None, out result); success = DateTime.TryParse(dateTimeString, cultureInfo.DateTimeFormat, DateTimeStyles.None, out result);
if (!success) if (!success)
result = currentDate; result = currentDate;
@ -53,15 +26,15 @@ namespace Microsoft.Windows.Controls
return success; return success;
} }
private string ResolveDateTimeString(string dateTime, DateTime currentDate) private static string ResolveDateTimeString(string dateTime, string format, DateTime currentDate, CultureInfo cultureInfo)
{ {
Dictionary<string, string> dateParts = GetDateParts(currentDate); Dictionary<string, string> dateParts = GetDateParts(currentDate, cultureInfo);
string[] timeParts = new string[3] { currentDate.Hour.ToString(), currentDate.Minute.ToString(), currentDate.Second.ToString() }; string[] timeParts = new string[3] { currentDate.Hour.ToString(), currentDate.Minute.ToString(), currentDate.Second.ToString() };
string designator = ""; string designator = "";
string[] dateTimeSeparators = new string[] { ",", " ", "-", ".", DateTimeFormatInfo.DateSeparator, DateTimeFormatInfo.TimeSeparator }; string[] dateTimeSeparators = new string[] { ",", " ", "-", ".", "/", cultureInfo.DateTimeFormat.DateSeparator, cultureInfo.DateTimeFormat.TimeSeparator };
var dateTimeParts = dateTime.Split(dateTimeSeparators, StringSplitOptions.RemoveEmptyEntries).ToList(); var dateTimeParts = dateTime.Split(dateTimeSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
var formats = Format.Split(dateTimeSeparators, StringSplitOptions.RemoveEmptyEntries).ToList(); var formats = format.Split(dateTimeSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
//something went wrong //something went wrong
if (dateTimeParts.Count != formats.Count) if (dateTimeParts.Count != formats.Count)
@ -69,41 +42,41 @@ namespace Microsoft.Windows.Controls
for (int i = 0; i < formats.Count; i++) for (int i = 0; i < formats.Count; i++)
{ {
var format = formats[i]; var f = formats[i];
if (!format.Contains("ddd") && !format.Contains("GMT")) if (!f.Contains("ddd") && !f.Contains("GMT"))
{ {
if (format.Contains("M")) if (f.Contains("M"))
dateParts["Month"] = dateTimeParts[i]; dateParts["Month"] = dateTimeParts[i];
else if (format.Contains("d")) else if (f.Contains("d"))
dateParts["Day"] = dateTimeParts[i]; dateParts["Day"] = dateTimeParts[i];
else if (format.Contains("y")) else if (f.Contains("y"))
{ {
dateParts["Year"] = dateTimeParts[i]; dateParts["Year"] = dateTimeParts[i];
if (dateParts["Year"].Length == 2) if (dateParts["Year"].Length == 2)
dateParts["Year"] = string.Format("{0}{1}", currentDate.Year / 100, dateParts["Year"]); dateParts["Year"] = string.Format("{0}{1}", currentDate.Year / 100, dateParts["Year"]);
} }
else if (format.Contains("h") || format.Contains("H")) else if (f.Contains("h") || f.Contains("H"))
timeParts[0] = dateTimeParts[i]; timeParts[0] = dateTimeParts[i];
else if (format.Contains("m")) else if (f.Contains("m"))
timeParts[1] = dateTimeParts[i]; timeParts[1] = dateTimeParts[i];
else if (format.Contains("s")) else if (f.Contains("s"))
timeParts[2] = dateTimeParts[i]; timeParts[2] = dateTimeParts[i];
else if (format.Contains("t")) else if (f.Contains("t"))
designator = dateTimeParts[i]; designator = dateTimeParts[i];
} }
} }
var date = string.Join(DateTimeFormatInfo.DateSeparator, dateParts.Select(x => x.Value).ToArray()); var date = string.Join(cultureInfo.DateTimeFormat.DateSeparator, dateParts.Select(x => x.Value).ToArray());
var time = string.Join(DateTimeFormatInfo.TimeSeparator, timeParts); var time = string.Join(cultureInfo.DateTimeFormat.TimeSeparator, timeParts);
return String.Format("{0} {1} {2}", date, time, designator); return String.Format("{0} {1} {2}", date, time, designator);
} }
private Dictionary<string, string> GetDateParts(DateTime currentDate) private static Dictionary<string, string> GetDateParts(DateTime currentDate, CultureInfo cultureInfo)
{ {
Dictionary<string, string> dateParts = new Dictionary<string, string>(); Dictionary<string, string> dateParts = new Dictionary<string, string>();
var dateFormatParts = DateTimeFormatInfo.ShortDatePattern.Split(new string[] { DateTimeFormatInfo.DateSeparator }, StringSplitOptions.RemoveEmptyEntries).ToList(); var dateFormatParts = cultureInfo.DateTimeFormat.ShortDatePattern.Split(new string[] { cultureInfo.DateTimeFormat.DateSeparator }, StringSplitOptions.RemoveEmptyEntries).ToList();
dateFormatParts.ForEach(item => dateFormatParts.ForEach(item =>
{ {
string key = string.Empty; string key = string.Empty;
@ -128,7 +101,5 @@ namespace Microsoft.Windows.Controls
}); });
return dateParts; return dateParts;
} }
#endregion // Methods
} }
} }

48
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/Implementation/DateTimeUpDown.cs

@ -17,14 +17,10 @@ namespace Microsoft.Windows.Controls
private bool _fireSelectionChangedEvent = true; private bool _fireSelectionChangedEvent = true;
private bool _processTextChanged = true; private bool _processTextChanged = true;
private DateTimeParser _dateTimeParser;
#endregion //Members #endregion //Members
#region Properties #region Properties
private DateTimeFormatInfo DateTimeFormatInfo { get; set; }
#region DefaultValue #region DefaultValue
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(DateTime), typeof(DateTimeUpDown), new UIPropertyMetadata(DateTime.Now)); public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register("DefaultValue", typeof(DateTime), typeof(DateTimeUpDown), new UIPropertyMetadata(DateTime.Now));
@ -54,7 +50,6 @@ namespace Microsoft.Windows.Controls
protected virtual void OnFormatChanged(DateTimeFormat oldValue, DateTimeFormat newValue) protected virtual void OnFormatChanged(DateTimeFormat oldValue, DateTimeFormat newValue)
{ {
_dateTimeParser.Format = GetFormatString(newValue);
InitializeDateTimeInfoListAndParseValue(); InitializeDateTimeInfoListAndParseValue();
UpdateTextFormatting(); UpdateTextFormatting();
} }
@ -82,7 +77,6 @@ namespace Microsoft.Windows.Controls
if (string.IsNullOrEmpty(newValue)) if (string.IsNullOrEmpty(newValue))
throw new ArgumentException("FormatString should be specified.", "newValue"); throw new ArgumentException("FormatString should be specified.", "newValue");
_dateTimeParser.Format = newValue;
InitializeDateTimeInfoListAndParseValue(); InitializeDateTimeInfoListAndParseValue();
UpdateTextFormatting(); UpdateTextFormatting();
} }
@ -112,8 +106,6 @@ namespace Microsoft.Windows.Controls
public DateTimeUpDown() public DateTimeUpDown()
{ {
DateTimeFormatInfo = DateTimeFormatInfo.GetInstance(CultureInfo);
_dateTimeParser = new DateTimeParser(DateTimeFormatInfo, GetFormatString(Format));
InitializeDateTimeInfoList(); InitializeDateTimeInfoList();
} }
@ -127,6 +119,11 @@ namespace Microsoft.Windows.Controls
TextBox.SelectionChanged += TextBox_SelectionChanged; TextBox.SelectionChanged += TextBox_SelectionChanged;
} }
protected override void OnCultureInfoChanged(CultureInfo oldValue, CultureInfo newValue)
{
InitializeDateTimeInfoList();
}
protected override DateTime? CoerceValue(DateTime? value) protected override DateTime? CoerceValue(DateTime? value)
{ {
//TODO: implement Minimum and Maximum //TODO: implement Minimum and Maximum
@ -187,11 +184,11 @@ namespace Microsoft.Windows.Controls
return; return;
} }
DateTime current = Value.HasValue ? Value.Value : DateTime.Now; DateTime current = Value.HasValue ? Value.Value : DateTime.Parse(DateTime.Now.ToString(), CultureInfo.DateTimeFormat);
DateTime result; DateTime result;
var success = _dateTimeParser.TryParse(currentValue, out result, current); var success = DateTimeParser.TryParse(currentValue, GetFormatString(Format), current, CultureInfo, out result);
SyncTextAndValueProperties(InputBase.TextProperty, result.ToString()); SyncTextAndValueProperties(InputBase.TextProperty, result.ToString(CultureInfo));
} }
protected override DateTime? ConvertTextToValue(string text) protected override DateTime? ConvertTextToValue(string text)
@ -199,15 +196,16 @@ namespace Microsoft.Windows.Controls
if (string.IsNullOrEmpty(text)) if (string.IsNullOrEmpty(text))
return null; return null;
return DateTime.Parse(text, CultureInfo.CurrentCulture); return DateTime.Parse(text, CultureInfo);
} }
protected override string ConvertValueToText() protected override string ConvertValueToText()
{ {
if (Value == null) return string.Empty; if (Value == null) return string.Empty;
DateTime dt = DateTime.Parse(Value.ToString(), CultureInfo); var test = Value.Value.ToString(CultureInfo);
return dt.ToString(GetFormatString(Format), CultureInfo);
return Value.Value.ToString(GetFormatString(Format), CultureInfo);
} }
protected override void SetValidSpinDirection() protected override void SetValidSpinDirection()
@ -437,7 +435,7 @@ namespace Microsoft.Windows.Controls
{ {
DateTime date = DateTime.Parse(Value.ToString()); DateTime date = DateTime.Parse(Value.ToString());
info.StartPosition = text.Length; info.StartPosition = text.Length;
info.Content = date.ToString(info.Format, DateTimeFormatInfo); info.Content = date.ToString(info.Format, CultureInfo.DateTimeFormat);
info.Length = info.Content.Length; info.Length = info.Content.Length;
text += info.Content; text += info.Content;
} }
@ -469,25 +467,25 @@ namespace Microsoft.Windows.Controls
switch (dateTimeFormat) switch (dateTimeFormat)
{ {
case DateTimeFormat.ShortDate: case DateTimeFormat.ShortDate:
return DateTimeFormatInfo.ShortDatePattern; return CultureInfo.DateTimeFormat.ShortDatePattern;
case DateTimeFormat.LongDate: case DateTimeFormat.LongDate:
return DateTimeFormatInfo.LongDatePattern; return CultureInfo.DateTimeFormat.LongDatePattern;
case DateTimeFormat.ShortTime: case DateTimeFormat.ShortTime:
return DateTimeFormatInfo.ShortTimePattern; return CultureInfo.DateTimeFormat.ShortTimePattern;
case DateTimeFormat.LongTime: case DateTimeFormat.LongTime:
return DateTimeFormatInfo.LongTimePattern; return CultureInfo.DateTimeFormat.LongTimePattern;
case DateTimeFormat.FullDateTime: case DateTimeFormat.FullDateTime:
return DateTimeFormatInfo.FullDateTimePattern; return CultureInfo.DateTimeFormat.FullDateTimePattern;
case DateTimeFormat.MonthDay: case DateTimeFormat.MonthDay:
return DateTimeFormatInfo.MonthDayPattern; return CultureInfo.DateTimeFormat.MonthDayPattern;
case DateTimeFormat.RFC1123: case DateTimeFormat.RFC1123:
return DateTimeFormatInfo.RFC1123Pattern; return CultureInfo.DateTimeFormat.RFC1123Pattern;
case DateTimeFormat.SortableDateTime: case DateTimeFormat.SortableDateTime:
return DateTimeFormatInfo.SortableDateTimePattern; return CultureInfo.DateTimeFormat.SortableDateTimePattern;
case DateTimeFormat.UniversalSortableDateTime: case DateTimeFormat.UniversalSortableDateTime:
return DateTimeFormatInfo.UniversalSortableDateTimePattern; return CultureInfo.DateTimeFormat.UniversalSortableDateTimePattern;
case DateTimeFormat.YearMonth: case DateTimeFormat.YearMonth:
return DateTimeFormatInfo.YearMonthPattern; return CultureInfo.DateTimeFormat.YearMonthPattern;
case DateTimeFormat.Custom: case DateTimeFormat.Custom:
return FormatString; return FormatString;
default: default:

Loading…
Cancel
Save