Browse Source

did some refactoring of DateTimeUpDown

pull/1645/head
brianlagunas_cp 16 years ago
parent
commit
bb9b091063
  1. 6
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeInfo.cs
  2. 423
      ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs

6
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeInfo.cs

@ -5,11 +5,9 @@ namespace Microsoft.Windows.Controls
internal class DateTimeInfo internal class DateTimeInfo
{ {
public string Content { get; set; } public string Content { get; set; }
public bool? IsReadOnly { get; set; } public string Format { get; set; }
public int KeyPressCount { get; set; } public bool IsReadOnly { get; set; }
public int Length { get; set; } public int Length { get; set; }
public string MonthName { get; set; }
public string Pattern { get; set; }
public int StartPosition { get; set; } public int StartPosition { get; set; }
public DateTimePart Type { get; set; } public DateTimePart Type { get; set; }
} }

423
ExtendedWPFToolkitSolution/Src/WPFToolkit.Extended/DateTimeUpDown/DateTimeUpDown.cs

@ -10,8 +10,8 @@ namespace Microsoft.Windows.Controls
#region Members #region Members
private List<DateTimeInfo> _dateTimeInfoList = new List<DateTimeInfo>(); private List<DateTimeInfo> _dateTimeInfoList = new List<DateTimeInfo>();
internal bool _selectionChanged = true; private DateTimeInfo _selectedDateTimeInfo;
internal int _selectedDateTimeInfoIndex; private bool _fireSelectionChangedEvent = true;
#endregion //Members #endregion //Members
@ -79,22 +79,54 @@ namespace Microsoft.Windows.Controls
void TextBox_SelectionChanged(object sender, RoutedEventArgs e) void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
{ {
if (_selectionChanged) if (_fireSelectionChangedEvent)
SelectDateTimePart(); SelectDateTimePart();
else else
_selectionChanged = true; _fireSelectionChangedEvent = true;
} }
#endregion //Event Hanlders #endregion //Event Hanlders
#region Methods #region Methods
#region Abstract
protected override void OnIncrement()
{
UpdateDateTime(1);
}
protected override void OnDecrement()
{
UpdateDateTime(-1);
}
protected override object ConvertTextToValue(string text)
{
throw new NotImplementedException("ConvertTextToValue");
}
protected override string ConvertValueToText(object value)
{
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;
}
#endregion //Abstract
private void InitializeDateTimeInfoList() private void InitializeDateTimeInfoList()
{ {
string format = GetFormatString(Format); string format = GetFormatString(Format);
while (format.Length > 0) while (format.Length > 0)
{ {
int elementLength = GetGroupLengthByMask(format); int elementLength = GetElementLengthByFormat(format);
DateTimeInfo info = null;
switch (format[0]) switch (format[0])
{ {
@ -102,17 +134,10 @@ namespace Microsoft.Windows.Controls
case '\'': case '\'':
{ {
int closingQuotePosition = format.IndexOf(format[0], 1); int closingQuotePosition = format.IndexOf(format[0], 1);
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = true, Type = DateTimePart.Other, Length = 1, Content = format.Substring(1, Math.Max(1, closingQuotePosition - 1)).ToString() };
{
IsReadOnly = true,
Type = DateTimePart.Other,
Length = 1,
Content = format.Substring(1, Math.Max(1, closingQuotePosition - 1)).ToString(),
});
elementLength = Math.Max(1, closingQuotePosition + 1); elementLength = Math.Max(1, closingQuotePosition + 1);
break; break;
} }
case 'D': case 'D':
case 'd': case 'd':
{ {
@ -121,23 +146,9 @@ namespace Microsoft.Windows.Controls
d = "%" + d; d = "%" + d;
if (elementLength > 2) if (elementLength > 2)
{ info = new DateTimeInfo { IsReadOnly = true, Type = DateTimePart.DayName, Format = d };
_dateTimeInfoList.Add(new DateTimeInfo()
{
IsReadOnly = true,
Type = DateTimePart.DayName,
Pattern = d
});
}
else else
{ info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Day, Format = d };
_dateTimeInfoList.Add(new DateTimeInfo()
{
IsReadOnly = false,
Type = DateTimePart.Day,
Pattern = d
});
}
break; break;
} }
case 'F': case 'F':
@ -147,12 +158,7 @@ namespace Microsoft.Windows.Controls
if (elementLength == 1) if (elementLength == 1)
f = "%" + f; f = "%" + f;
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Millisecond, Format = f };
{
IsReadOnly = false,
Type = DateTimePart.Millisecond,
Pattern = f
});
break; break;
} }
case 'h': case 'h':
@ -161,12 +167,7 @@ namespace Microsoft.Windows.Controls
if (elementLength == 1) if (elementLength == 1)
h = "%" + h; h = "%" + h;
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Hour12, Format = h };
{
IsReadOnly = false,
Type = DateTimePart.Hour12,
Pattern = h
});
break; break;
} }
case 'H': case 'H':
@ -175,12 +176,7 @@ namespace Microsoft.Windows.Controls
if (elementLength == 1) if (elementLength == 1)
H = "%" + H; H = "%" + H;
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Hour24, Format = H };
{
IsReadOnly = false,
Type = DateTimePart.Hour24,
Pattern = H
});
break; break;
} }
case 'M': case 'M':
@ -190,23 +186,9 @@ namespace Microsoft.Windows.Controls
M = "%" + M; M = "%" + M;
if (elementLength >= 3) if (elementLength >= 3)
{ info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.MonthName, Format = M };
_dateTimeInfoList.Add(new DateTimeInfo()
{
IsReadOnly = false,
Type = DateTimePart.MonthName,
Pattern = M
});
}
else else
{ info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Month, Format = M };
_dateTimeInfoList.Add(new DateTimeInfo()
{
IsReadOnly = false,
Type = DateTimePart.Month,
Pattern = M
});
}
break; break;
} }
case 'S': case 'S':
@ -215,12 +197,8 @@ namespace Microsoft.Windows.Controls
string s = format.Substring(0, elementLength); string s = format.Substring(0, elementLength);
if (elementLength == 1) if (elementLength == 1)
s = "%" + s; s = "%" + s;
_dateTimeInfoList.Add(new DateTimeInfo()
{ info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Second, Format = s };
IsReadOnly = false,
Type = DateTimePart.Second,
Pattern = s
});
break; break;
} }
case 'T': case 'T':
@ -228,15 +206,9 @@ namespace Microsoft.Windows.Controls
{ {
string t = format.Substring(0, elementLength); string t = format.Substring(0, elementLength);
if (elementLength == 1) if (elementLength == 1)
{
t = "%" + t; t = "%" + t;
}
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.AmPmDesignator, Format = t };
{
IsReadOnly = false,
Type = DateTimePart.AmPmDesignator,
Pattern = t
});
break; break;
} }
case 'Y': case 'Y':
@ -244,28 +216,16 @@ namespace Microsoft.Windows.Controls
{ {
string y = format.Substring(0, elementLength); string y = format.Substring(0, elementLength);
if (elementLength == 1) if (elementLength == 1)
{
y = "%" + y; y = "%" + y;
}
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Year, Format = y };
{
IsReadOnly = false,
Type = DateTimePart.Year,
Pattern = y
});
break; break;
} }
case '\\': case '\\':
{ {
if (format.Length >= 2) if (format.Length >= 2)
{ {
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = true, Content = format.Substring(1, 1), Length = 1, Type = DateTimePart.Other };
{
IsReadOnly = true,
Content = format.Substring(1, 1),
Length = 1,
Type = DateTimePart.Other
});
elementLength = 2; elementLength = 2;
} }
break; break;
@ -274,149 +234,90 @@ namespace Microsoft.Windows.Controls
{ {
string g = format.Substring(0, elementLength); string g = format.Substring(0, elementLength);
if (elementLength == 1) if (elementLength == 1)
{
g = "%" + g; g = "%" + g;
}
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = true, Type = DateTimePart.Period, Format = format.Substring(0, elementLength) };
{
IsReadOnly = true,
Type = DateTimePart.Period,
Pattern = format.Substring(0, elementLength)
});
break; break;
} }
case 'm': case 'm':
{ {
string m = format.Substring(0, elementLength); string m = format.Substring(0, elementLength);
if (elementLength == 1) if (elementLength == 1)
{
m = "%" + m; m = "%" + m;
}
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = false, Type = DateTimePart.Minute, Format = m };
{
IsReadOnly = false,
Type = DateTimePart.Minute,
Pattern = m
});
break; break;
} }
case 'z': case 'z':
{ {
string z = format.Substring(0, elementLength); string z = format.Substring(0, elementLength);
if (elementLength == 1) if (elementLength == 1)
{
z = "%" + z; z = "%" + z;
}
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = true, Type = DateTimePart.TimeZone, Format = z };
{
IsReadOnly = true,
Type = DateTimePart.TimeZone,
Pattern = z
});
break; break;
} }
default: default:
{ {
elementLength = 1; elementLength = 1;
_dateTimeInfoList.Add(new DateTimeInfo() info = new DateTimeInfo { IsReadOnly = true, Length = 1, Content = format[0].ToString(), Type = DateTimePart.Other };
{
IsReadOnly = true,
Length = 1,
Content = format[0].ToString(),
Type = DateTimePart.Other
});
break; break;
} }
} }
_dateTimeInfoList.Add(info);
format = format.Substring(elementLength); format = format.Substring(elementLength);
} }
} }
private static int GetGroupLengthByMask(string mask) private static int GetElementLengthByFormat(string format)
{ {
for (int i = 1; i < mask.Length; i++) for (int i = 1; i < format.Length; i++)
{ {
if (String.Compare(mask[i].ToString(), mask[0].ToString(), false) != 0) if (String.Compare(format[i].ToString(), format[0].ToString(), false) != 0)
{ {
return i; return i;
} }
} }
return mask.Length; return format.Length;
} }
public void ParseValueIntoDateTimeInfo() private void ParseValueIntoDateTimeInfo()
{ {
string displayText = string.Empty; string text = string.Empty;
for (int i = 0; i < _dateTimeInfoList.Count; i++) _dateTimeInfoList.ForEach(info =>
{ {
var charProperty = _dateTimeInfoList[i]; if (info.Format == null)
if (charProperty.Pattern == null)
{ {
_dateTimeInfoList[i].StartPosition = displayText.Length; info.StartPosition = text.Length;
_dateTimeInfoList[i].Length = charProperty.Content.Length; info.Length = info.Content.Length;
displayText += charProperty.Content; text += info.Content;
} }
else else
{ {
DateTime date = DateTime.Parse(Value.ToString()); DateTime date = DateTime.Parse(Value.ToString());
info.StartPosition = text.Length;
_dateTimeInfoList[i].StartPosition = displayText.Length; info.Content = date.ToString(info.Format, DateTimeFormatInfo);
info.Length = info.Content.Length;
charProperty.Content = date.ToString(charProperty.Pattern, DateTimeFormatInfo); text += info.Content;
_dateTimeInfoList[i].Length = charProperty.Content.Length;
displayText += charProperty.Content;
} }
} });
} }
private void SelectDateTimePart() private void SelectDateTimePart()
{ {
for (int i = 0; i < _dateTimeInfoList.Count; i++) _dateTimeInfoList.ForEach(info =>
{ {
var charProperty = _dateTimeInfoList[i]; if ((info.StartPosition <= TextBox.SelectionStart) && (TextBox.SelectionStart < (info.StartPosition + info.Length)))
if ((charProperty.StartPosition <= TextBox.SelectionStart) && (TextBox.SelectionStart < (charProperty.StartPosition + charProperty.Length)))
{ {
if (charProperty.IsReadOnly == false) _fireSelectionChangedEvent = false;
{ TextBox.Select(info.StartPosition, info.Length);
_selectionChanged = false; _fireSelectionChangedEvent = true;
_selectedDateTimeInfo = info;
for (int j = 0; j < _dateTimeInfoList.Count; j++) return;
{
_dateTimeInfoList[j].KeyPressCount = 0;
}
TextBox.Select(charProperty.StartPosition, charProperty.Length);
_selectionChanged = true;
_selectedDateTimeInfoIndex = i;
return;
}
else
{
_selectionChanged = false;
for (int j = 0; j < _dateTimeInfoList.Count; j++)
{
_dateTimeInfoList[j].KeyPressCount = 0;
}
TextBox.Select(charProperty.StartPosition, charProperty.Length);
_selectionChanged = true;
_selectedDateTimeInfoIndex = i;
return;
}
} }
} });
_selectionChanged = false;
TextBox.Select(TextBox.SelectionStart, 0);
_selectionChanged = true;
_selectedDateTimeInfoIndex = -1;
} }
private string GetFormatString(DateTimeFormat dateTimeFormat) private string GetFormatString(DateTimeFormat dateTimeFormat)
@ -448,118 +349,66 @@ namespace Microsoft.Windows.Controls
} }
} }
#region Abstract private void UpdateDateTime(int value)
protected override void OnIncrement()
{
DateTimeInfo info = _dateTimeInfoList[_selectedDateTimeInfoIndex];
_selectionChanged = false;
//TODO: refactor
if (info.Type == DateTimePart.Year)
{
Value = ((DateTime)Value).AddYears(1);
}
else if (info.Type == DateTimePart.Month || info.Type == DateTimePart.MonthName)
{
Value = ((DateTime)Value).AddMonths(1);
}
else if (info.Type == DateTimePart.Day || info.Type == DateTimePart.DayName)
{
Value = ((DateTime)Value).AddDays(1);
}
else if (info.Type == DateTimePart.Hour12 || info.Type == DateTimePart.Hour24)
{
Value = ((DateTime)Value).AddHours(1);
}
else if (info.Type == DateTimePart.Minute)
{
Value = ((DateTime)Value).AddMinutes(1);
}
else if (info.Type == DateTimePart.Second)
{
Value = ((DateTime)Value).AddSeconds(1);
}
else if (info.Type == DateTimePart.Millisecond)
{
Value = ((DateTime)Value).AddMilliseconds(1);
}
else if (info.Type == DateTimePart.AmPmDesignator)
{
Value = ((DateTime)Value).AddHours(12);
}
//we loose our selection when the Value is set so we need to reselect it without firing the selection changed event
TextBox.Select(info.StartPosition, info.Length);
_selectionChanged = true;
}
protected override void OnDecrement()
{ {
DateTimeInfo info = _dateTimeInfoList[_selectedDateTimeInfoIndex]; _fireSelectionChangedEvent = false;
DateTimeInfo info = _selectedDateTimeInfo;
_selectionChanged = false; switch (info.Type)
//TODO: refactor
if (info.Type == DateTimePart.Year)
{
Value = ((DateTime)Value).AddYears(-1);
}
else if (info.Type == DateTimePart.Month || info.Type == DateTimePart.MonthName)
{
Value = ((DateTime)Value).AddMonths(-1);
}
else if (info.Type == DateTimePart.Day || info.Type == DateTimePart.DayName)
{
Value = ((DateTime)Value).AddDays(-1);
}
else if (info.Type == DateTimePart.Hour12 || info.Type == DateTimePart.Hour24)
{
Value = ((DateTime)Value).AddHours(-1);
}
else if (info.Type == DateTimePart.Minute)
{
Value = ((DateTime)Value).AddMinutes(-1);
}
else if (info.Type == DateTimePart.Second)
{ {
Value = ((DateTime)Value).AddSeconds(-1); case DateTimePart.Year:
} {
else if (info.Type == DateTimePart.Millisecond) Value = ((DateTime)Value).AddYears(value);
{ break;
Value = ((DateTime)Value).AddMilliseconds(-1); }
} case DateTimePart.Month:
else if (info.Type == DateTimePart.AmPmDesignator) case DateTimePart.MonthName:
{ {
Value = ((DateTime)Value).AddHours(-12); Value = ((DateTime)Value).AddMonths(value);
break;
}
case DateTimePart.Day:
case DateTimePart.DayName:
{
Value = ((DateTime)Value).AddDays(value);
break;
}
case DateTimePart.Hour12:
case DateTimePart.Hour24:
{
Value = ((DateTime)Value).AddHours(value);
break;
}
case DateTimePart.Minute:
{
Value = ((DateTime)Value).AddMinutes(value);
break;
}
case DateTimePart.Second:
{
Value = ((DateTime)Value).AddSeconds(value);
break;
}
case DateTimePart.Millisecond:
{
Value = ((DateTime)Value).AddMilliseconds(value);
break;
}
case DateTimePart.AmPmDesignator:
{
Value = ((DateTime)Value).AddHours(value * 12);
break;
}
default:
{
break;
}
} }
//we loose our selection when the Value is set so we need to reselect it without firing the selection changed event //we loose our selection when the Value is set so we need to reselect it without firing the selection changed event
TextBox.Select(info.StartPosition, info.Length); TextBox.Select(info.StartPosition, info.Length);
_fireSelectionChangedEvent = true;
_selectionChanged = true;
}
protected override object ConvertTextToValue(string text)
{
return text;
}
protected override string ConvertValueToText(object value)
{
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;
} }
#endregion //Abstract
#endregion //Methods #endregion //Methods
} }
} }

Loading…
Cancel
Save