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
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
{
get { return (CultureInfo)GetValue(CultureInfoProperty); }
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
#region IsReadOnly

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

@ -1,51 +1,24 @@
using System;
using System.Linq;
using System.Globalization;
using System.Text;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Globalization;
using System.Linq;
namespace Microsoft.Windows.Controls
{
internal class DateTimeParser
{
#region Properties
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)
public static bool TryParse(string value, string format, DateTime currentDate, CultureInfo cultureInfo, out DateTime result)
{
bool success = false;
result = currentDate;
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(Format))
if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(format))
return false;
var dateTimeString = ResolveDateTimeString(value, currentDate);
var dateTimeString = ResolveDateTimeString(value, format, currentDate, cultureInfo).Trim();
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)
result = currentDate;
@ -53,15 +26,15 @@ namespace Microsoft.Windows.Controls
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 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 formats = Format.Split(dateTimeSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
var formats = format.Split(dateTimeSeparators, StringSplitOptions.RemoveEmptyEntries).ToList();
//something went wrong
if (dateTimeParts.Count != formats.Count)
@ -69,41 +42,41 @@ namespace Microsoft.Windows.Controls
for (int i = 0; i < formats.Count; i++)
{
var format = formats[i];
if (!format.Contains("ddd") && !format.Contains("GMT"))
var f = formats[i];
if (!f.Contains("ddd") && !f.Contains("GMT"))
{
if (format.Contains("M"))
if (f.Contains("M"))
dateParts["Month"] = dateTimeParts[i];
else if (format.Contains("d"))
else if (f.Contains("d"))
dateParts["Day"] = dateTimeParts[i];
else if (format.Contains("y"))
else if (f.Contains("y"))
{
dateParts["Year"] = dateTimeParts[i];
if (dateParts["Year"].Length == 2)
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];
else if (format.Contains("m"))
else if (f.Contains("m"))
timeParts[1] = dateTimeParts[i];
else if (format.Contains("s"))
else if (f.Contains("s"))
timeParts[2] = dateTimeParts[i];
else if (format.Contains("t"))
else if (f.Contains("t"))
designator = dateTimeParts[i];
}
}
var date = string.Join(DateTimeFormatInfo.DateSeparator, dateParts.Select(x => x.Value).ToArray());
var time = string.Join(DateTimeFormatInfo.TimeSeparator, timeParts);
var date = string.Join(cultureInfo.DateTimeFormat.DateSeparator, dateParts.Select(x => x.Value).ToArray());
var time = string.Join(cultureInfo.DateTimeFormat.TimeSeparator, timeParts);
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>();
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 =>
{
string key = string.Empty;
@ -128,7 +101,5 @@ namespace Microsoft.Windows.Controls
});
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 _processTextChanged = true;
private DateTimeParser _dateTimeParser;
#endregion //Members
#region Properties
private DateTimeFormatInfo DateTimeFormatInfo { get; set; }
#region DefaultValue
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)
{
_dateTimeParser.Format = GetFormatString(newValue);
InitializeDateTimeInfoListAndParseValue();
UpdateTextFormatting();
}
@ -82,7 +77,6 @@ namespace Microsoft.Windows.Controls
if (string.IsNullOrEmpty(newValue))
throw new ArgumentException("FormatString should be specified.", "newValue");
_dateTimeParser.Format = newValue;
InitializeDateTimeInfoListAndParseValue();
UpdateTextFormatting();
}
@ -112,8 +106,6 @@ namespace Microsoft.Windows.Controls
public DateTimeUpDown()
{
DateTimeFormatInfo = DateTimeFormatInfo.GetInstance(CultureInfo);
_dateTimeParser = new DateTimeParser(DateTimeFormatInfo, GetFormatString(Format));
InitializeDateTimeInfoList();
}
@ -127,6 +119,11 @@ namespace Microsoft.Windows.Controls
TextBox.SelectionChanged += TextBox_SelectionChanged;
}
protected override void OnCultureInfoChanged(CultureInfo oldValue, CultureInfo newValue)
{
InitializeDateTimeInfoList();
}
protected override DateTime? CoerceValue(DateTime? value)
{
//TODO: implement Minimum and Maximum
@ -187,11 +184,11 @@ namespace Microsoft.Windows.Controls
return;
}
DateTime current = Value.HasValue ? Value.Value : DateTime.Now;
DateTime current = Value.HasValue ? Value.Value : DateTime.Parse(DateTime.Now.ToString(), CultureInfo.DateTimeFormat);
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)
@ -199,15 +196,16 @@ namespace Microsoft.Windows.Controls
if (string.IsNullOrEmpty(text))
return null;
return DateTime.Parse(text, CultureInfo.CurrentCulture);
return DateTime.Parse(text, CultureInfo);
}
protected override string ConvertValueToText()
{
if (Value == null) return string.Empty;
DateTime dt = DateTime.Parse(Value.ToString(), CultureInfo);
return dt.ToString(GetFormatString(Format), CultureInfo);
var test = Value.Value.ToString(CultureInfo);
return Value.Value.ToString(GetFormatString(Format), CultureInfo);
}
protected override void SetValidSpinDirection()
@ -437,7 +435,7 @@ namespace Microsoft.Windows.Controls
{
DateTime date = DateTime.Parse(Value.ToString());
info.StartPosition = text.Length;
info.Content = date.ToString(info.Format, DateTimeFormatInfo);
info.Content = date.ToString(info.Format, CultureInfo.DateTimeFormat);
info.Length = info.Content.Length;
text += info.Content;
}
@ -469,25 +467,25 @@ namespace Microsoft.Windows.Controls
switch (dateTimeFormat)
{
case DateTimeFormat.ShortDate:
return DateTimeFormatInfo.ShortDatePattern;
return CultureInfo.DateTimeFormat.ShortDatePattern;
case DateTimeFormat.LongDate:
return DateTimeFormatInfo.LongDatePattern;
return CultureInfo.DateTimeFormat.LongDatePattern;
case DateTimeFormat.ShortTime:
return DateTimeFormatInfo.ShortTimePattern;
return CultureInfo.DateTimeFormat.ShortTimePattern;
case DateTimeFormat.LongTime:
return DateTimeFormatInfo.LongTimePattern;
return CultureInfo.DateTimeFormat.LongTimePattern;
case DateTimeFormat.FullDateTime:
return DateTimeFormatInfo.FullDateTimePattern;
return CultureInfo.DateTimeFormat.FullDateTimePattern;
case DateTimeFormat.MonthDay:
return DateTimeFormatInfo.MonthDayPattern;
return CultureInfo.DateTimeFormat.MonthDayPattern;
case DateTimeFormat.RFC1123:
return DateTimeFormatInfo.RFC1123Pattern;
return CultureInfo.DateTimeFormat.RFC1123Pattern;
case DateTimeFormat.SortableDateTime:
return DateTimeFormatInfo.SortableDateTimePattern;
return CultureInfo.DateTimeFormat.SortableDateTimePattern;
case DateTimeFormat.UniversalSortableDateTime:
return DateTimeFormatInfo.UniversalSortableDateTimePattern;
return CultureInfo.DateTimeFormat.UniversalSortableDateTimePattern;
case DateTimeFormat.YearMonth:
return DateTimeFormatInfo.YearMonthPattern;
return CultureInfo.DateTimeFormat.YearMonthPattern;
case DateTimeFormat.Custom:
return FormatString;
default:

Loading…
Cancel
Save