Exemple #1
0
        private void ApplyFormat()
        {
            _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(Format, true);

            IsYearShortFormat = _formatInfo.IsYearShortFormat;
            NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1));
            NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000));

            EnableOrDisableYearConverterDependingOnFormat();

            NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length));
            NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length));
            NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length));

            UnsubscribeNumericTextBoxes();

            Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition));
            Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition));
            Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition));

            Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1);
            Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1);
            Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1);

            _textBoxes[_formatInfo.DayPosition]   = NumericTBDay;
            _textBoxes[_formatInfo.MonthPosition] = NumericTBMonth;
            _textBoxes[_formatInfo.YearPosition]  = NumericTBYear;

            // Fix tab order inside control.
            NumericTBDay.SetCurrentValue(TabIndexProperty, _formatInfo.DayPosition);
            NumericTBMonth.SetCurrentValue(TabIndexProperty, _formatInfo.MonthPosition);
            NumericTBYear.SetCurrentValue(TabIndexProperty, _formatInfo.YearPosition);
            DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue);

            SubscribeNumericTextBoxes();

            Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1);
            Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2);
            Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3);
        }
Exemple #2
0
        private void ApplyFormat()
        {
            _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(Format, false);

            IsYearShortFormat = _formatInfo.IsYearShortFormat;
            NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1));
            NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000));

            IsHour12Format = _formatInfo.IsHour12Format.Value;
            NumericTBHour.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsHour12Format.Value ? 1 : 0));
            NumericTBHour.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsHour12Format.Value ? 12 : 23));
            ToggleButtonH.SetCurrentValue(TagProperty, _formatInfo.IsHour12Format.Value ? DateTimePart.Hour12 : DateTimePart.Hour);

            IsAmPmShortFormat = _formatInfo.IsAmPmShortFormat.Value;

            EnableOrDisableYearConverterDependingOnFormat();
            EnableOrDisableHourConverterDependingOnFormat();
            EnableOrDisableAmPmConverterDependingOnFormat();

            ListTBAmPm.SetCurrentValue(ListTextBox.ListOfValuesProperty, new List <string>()
            {
                Meridiems.GetAmForFormat(_formatInfo),
                Meridiems.GetPmForFormat(_formatInfo)
            });

            NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length));
            NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length));
            NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length));
            NumericTBHour.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.HourFormat.Length));
            NumericTBMinute.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MinuteFormat.Length));
            NumericTBSecond.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.SecondFormat.Length));

            UnsubscribeNumericTextBoxes();

            Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition));
            Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition));
            Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition));
            Grid.SetColumn(NumericTBHour, GetPosition(_formatInfo.HourPosition.Value));
            Grid.SetColumn(NumericTBMinute, GetPosition(_formatInfo.MinutePosition.Value));
            Grid.SetColumn(NumericTBSecond, GetPosition(_formatInfo.SecondPosition.Value));
            Grid.SetColumn(ListTBAmPm, GetPosition(_formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value));

            Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1);
            Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1);
            Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1);
            Grid.SetColumn(ToggleButtonH, GetPosition(_formatInfo.HourPosition.Value) + 1);
            Grid.SetColumn(ToggleButtonM, GetPosition(_formatInfo.MinutePosition.Value) + 1);
            Grid.SetColumn(ToggleButtonS, GetPosition(_formatInfo.SecondPosition.Value) + 1);
            Grid.SetColumn(ToggleButtonT, GetPosition(_formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value) + 1);

            // Fix positions which could be broken, because of AM/PM textblock.
            int dayPos = _formatInfo.DayPosition, monthPos = _formatInfo.MonthPosition, yearPos = _formatInfo.YearPosition,
                hourPos = _formatInfo.HourPosition.Value, minutePos = _formatInfo.MinutePosition.Value, secondPos = _formatInfo.SecondPosition.Value,
                amPmPos = _formatInfo.AmPmPosition.HasValue == false ? 6 : _formatInfo.AmPmPosition.Value;

            FixNumericTextBoxesPositions(ref dayPos, ref monthPos, ref yearPos, ref hourPos, ref minutePos, ref secondPos, ref amPmPos);

            _textBoxes[dayPos]    = NumericTBDay;
            _textBoxes[monthPos]  = NumericTBMonth;
            _textBoxes[yearPos]   = NumericTBYear;
            _textBoxes[hourPos]   = NumericTBHour;
            _textBoxes[minutePos] = NumericTBMinute;
            _textBoxes[secondPos] = NumericTBSecond;
            _textBoxes[amPmPos]   = ListTBAmPm;

            // Fix tab order inside control.
            NumericTBDay.SetCurrentValue(TabIndexProperty, dayPos);
            NumericTBMonth.SetCurrentValue(TabIndexProperty, monthPos);
            NumericTBYear.SetCurrentValue(TabIndexProperty, yearPos);
            NumericTBHour.SetCurrentValue(TabIndexProperty, hourPos);
            NumericTBMinute.SetCurrentValue(TabIndexProperty, minutePos);
            NumericTBSecond.SetCurrentValue(TabIndexProperty, secondPos);
            ListTBAmPm.SetCurrentValue(TabIndexProperty, amPmPos);
            DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue);

            SubscribeNumericTextBoxes();

            Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1);
            Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2);
            Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3);
            Separator4.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator4);
            Separator5.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator5);
            Separator6.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator6);
            Separator7.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator7);
        }
Exemple #3
0
        internal static string Format(DateTime dateTime, DateTimeFormatInfo formatInfo)
        {
            Argument.IsNotNull(() => formatInfo);

            var parts = new List <KeyValuePair <int, string> >
            {
                new KeyValuePair <int, string>(formatInfo.DayPosition, dateTime.Day.ToString(NumberFormatHelper.GetFormat(formatInfo.DayFormat.Length))),
                new KeyValuePair <int, string>(formatInfo.MonthPosition, dateTime.Month.ToString(NumberFormatHelper.GetFormat(formatInfo.MonthFormat.Length)))
            };

            if (formatInfo.IsYearShortFormat)
            {
                var yearShort = dateTime.Year - (dateTime.Year - dateTime.Year % 100);
                parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, yearShort.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length))));
            }
            else
            {
                parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, dateTime.Year.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length))));
            }

            if (!formatInfo.IsDateOnly)
            {
                if (formatInfo.IsHour12Format == true)
                {
                    var hour12 = dateTime.Hour % 12 > 0 ? dateTime.Hour % 12 : 12;
                    parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, hour12.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length))));
                }
                else
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, dateTime.Hour.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length))));
                }

                parts.Add(new KeyValuePair <int, string>(formatInfo.MinutePosition.Value, dateTime.Minute.ToString(NumberFormatHelper.GetFormat(formatInfo.MinuteFormat.Length))));

                if (formatInfo.SecondPosition.HasValue)
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.SecondPosition.Value, dateTime.Second.ToString(NumberFormatHelper.GetFormat(formatInfo.SecondFormat.Length))));
                }

                if (formatInfo.AmPmPosition.HasValue)
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.AmPmPosition.Value, dateTime.Hour >= 12 ? Meridiems.GetPmForFormat(formatInfo) : Meridiems.GetAmForFormat(formatInfo)));
                }
            }

            parts = parts.OrderBy(x => x.Key).ToList();

            return(BuildFormatString(formatInfo, parts));
        }
        internal static string Format(DateTime dateTime, DateTimeFormatInfo formatInfo)
        {
            Argument.IsNotNull(() => formatInfo);

            var parts = new List <KeyValuePair <int, string> >
            {
                new KeyValuePair <int, string>(formatInfo.DayPosition, dateTime.Day.ToString(NumberFormatHelper.GetFormat(formatInfo.DayFormat.Length))),
                new KeyValuePair <int, string>(formatInfo.MonthPosition, dateTime.Month.ToString(NumberFormatHelper.GetFormat(formatInfo.MonthFormat.Length)))
            };

            if (formatInfo.IsYearShortFormat)
            {
                var yearShort = dateTime.Year - (dateTime.Year - dateTime.Year % 100);
                parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, yearShort.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length))));
            }
            else
            {
                parts.Add(new KeyValuePair <int, string>(formatInfo.YearPosition, dateTime.Year.ToString(NumberFormatHelper.GetFormat(formatInfo.YearFormat.Length))));
            }

            if (!formatInfo.IsDateOnly)
            {
                if (formatInfo.IsHour12Format == true)
                {
                    var hour12 = dateTime.Hour % 12 > 0 ? dateTime.Hour % 12 : 12;
                    parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, hour12.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length))));
                }
                else
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.HourPosition.Value, dateTime.Hour.ToString(NumberFormatHelper.GetFormat(formatInfo.HourFormat.Length))));
                }
                parts.Add(new KeyValuePair <int, string>(formatInfo.MinutePosition.Value, dateTime.Minute.ToString(NumberFormatHelper.GetFormat(formatInfo.MinuteFormat.Length))));
                parts.Add(new KeyValuePair <int, string>(formatInfo.SecondPosition.Value, dateTime.Second.ToString(NumberFormatHelper.GetFormat(formatInfo.SecondFormat.Length))));
                if (formatInfo.AmPmPosition.HasValue)
                {
                    parts.Add(new KeyValuePair <int, string>(formatInfo.AmPmPosition.Value, dateTime.Hour >= 12 ? Meridiems.GetPmForFormat(formatInfo) : Meridiems.GetAmForFormat(formatInfo)));
                }
            }

            parts = parts.OrderBy(x => x.Key).ToList();

            // Always contain year, month, day part.
            var builder = new StringBuilder();

            builder.Append(formatInfo.Separator0);
            builder.Append(parts[0].Value);
            builder.Append(formatInfo.Separator1);
            builder.Append(parts[1].Value);
            builder.Append(formatInfo.Separator2);
            builder.Append(parts[2].Value);
            builder.Append(formatInfo.Separator3);

            if (parts.Count <= 3)
            {
                return(builder.ToString());
            }

            builder.Append(parts[3].Value);
            builder.Append(formatInfo.Separator4);
            builder.Append(parts[4].Value);
            builder.Append(formatInfo.Separator5);
            builder.Append(parts[5].Value);
            builder.Append(formatInfo.Separator6);

            if (parts.Count <= 6)
            {
                return(builder.ToString());
            }

            builder.Append(parts[6].Value);
            builder.Append(formatInfo.Separator7);

            return(builder.ToString());
        }
Exemple #5
0
        private void ApplyFormat()
        {
            _applyingFormat = true;

            try
            {
                var format = Format;
                _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(format);
                var hasLongTimeFormat = !(_formatInfo.HourFormat is null ||
                                          _formatInfo.MinuteFormat is null ||
                                          _formatInfo.SecondFormat is null);

                var hasAnyTimeFormat = !(_formatInfo.HourFormat is null &&
                                         _formatInfo.MinuteFormat is null &&
                                         _formatInfo.SecondFormat is null);

                SetCurrentValue(HideTimeProperty, !hasAnyTimeFormat || _hideTime);

                if (!hasLongTimeFormat)
                {
                    var timePattern = DateTimeFormatHelper.ExtractTimePatternFromFormat(format);
                    if (!string.IsNullOrEmpty(timePattern))
                    {
                        timePattern = DateTimeFormatHelper.FindMatchedLongTimePattern(CultureInfo.CurrentUICulture, timePattern);
                    }

                    if (string.IsNullOrEmpty(timePattern))
                    {
                        timePattern = CultureInfo.CurrentUICulture.DateTimeFormat.LongTimePattern;
                    }

                    var datePattern = DateTimeFormatHelper.ExtractDatePatternFromFormat(format);

                    format = $"{datePattern} {timePattern}";

                    _formatInfo = DateTimeFormatHelper.GetDateTimeFormatInfo(format);
                }

                if (!hasAnyTimeFormat)
                {
                }

                IsYearShortFormat = _formatInfo.IsYearShortFormat;
                NumericTBYear.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(_formatInfo.IsYearShortFormat ? 0 : 1));
                NumericTBYear.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(_formatInfo.IsYearShortFormat ? 99 : 3000));

                var isHour12Format = _formatInfo.IsHour12Format ?? true;
                IsHour12Format = isHour12Format;
                NumericTBHour.SetCurrentValue(NumericTextBox.MinValueProperty, (double)(isHour12Format ? 1 : 0));
                NumericTBHour.SetCurrentValue(NumericTextBox.MaxValueProperty, (double)(isHour12Format ? 12 : 23));
                ToggleButtonH.SetCurrentValue(TagProperty, isHour12Format ? DateTimePart.Hour12 : DateTimePart.Hour);

                IsAmPmShortFormat = _formatInfo.IsAmPmShortFormat.Value;

                EnableOrDisableYearConverterDependingOnFormat();
                EnableOrDisableHourConverterDependingOnFormat();
                EnableOrDisableAmPmConverterDependingOnFormat();

                ListTBAmPm.SetCurrentValue(ListTextBox.ListOfValuesProperty, new List <string>()
                {
                    Meridiems.GetAmForFormat(_formatInfo),
                    Meridiems.GetPmForFormat(_formatInfo)
                });

                NumericTBDay.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.DayFormat.Length));
                NumericTBMonth.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MonthFormat.Length));
                NumericTBYear.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.YearFormat.Length));
                NumericTBHour.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.HourFormat.Length));
                NumericTBMinute.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.MinuteFormat.Length));
                NumericTBSecond.SetCurrentValue(NumericTextBox.FormatProperty, NumberFormatHelper.GetFormat(_formatInfo.SecondFormat?.Length ?? 0));

                UnsubscribeNumericTextBoxes();

                Grid.SetColumn(NumericTBDay, GetPosition(_formatInfo.DayPosition));
                Grid.SetColumn(NumericTBMonth, GetPosition(_formatInfo.MonthPosition));
                Grid.SetColumn(NumericTBYear, GetPosition(_formatInfo.YearPosition));
                Grid.SetColumn(NumericTBHour, GetPosition(_formatInfo.HourPosition.Value));
                Grid.SetColumn(NumericTBMinute, GetPosition(_formatInfo.MinutePosition.Value));
                Grid.SetColumn(NumericTBSecond, GetPosition(_formatInfo.SecondPosition ?? _defaultSecondFormatPosition));
                Grid.SetColumn(ListTBAmPm, GetPosition(_formatInfo.AmPmPosition ?? _defaultAmPmFormatPosition));

                Grid.SetColumn(ToggleButtonD, GetPosition(_formatInfo.DayPosition) + 1);
                Grid.SetColumn(ToggleButtonMo, GetPosition(_formatInfo.MonthPosition) + 1);
                Grid.SetColumn(ToggleButtonY, GetPosition(_formatInfo.YearPosition) + 1);
                Grid.SetColumn(ToggleButtonH, GetPosition(_formatInfo.HourPosition.Value) + 1);
                Grid.SetColumn(ToggleButtonM, GetPosition(_formatInfo.MinutePosition.Value) + 1);
                Grid.SetColumn(ToggleButtonS, GetPosition(_formatInfo.SecondPosition ?? _defaultSecondFormatPosition) + 1);

                //hide parts according to Hide options and format
                bool isHiddenManual = HideSeconds || HideTime;

                NumericTBSecond.SetCurrentValue(NumericTextBox.VisibilityProperty, _formatInfo.SecondFormat is null || isHiddenManual ? Visibility.Collapsed : Visibility.Visible);
                ToggleButtonS.SetCurrentValue(NumericTextBox.VisibilityProperty, _formatInfo.SecondFormat is null || isHiddenManual ? Visibility.Collapsed : Visibility.Visible);

                Grid.SetColumn(ToggleButtonT, GetPosition((_formatInfo.AmPmPosition ?? _defaultAmPmFormatPosition) + 1));

                // Fix positions which could be broken, because of AM/PM textblock.
                // Fix for seconds in a same way
                int dayPos = _formatInfo.DayPosition, monthPos = _formatInfo.MonthPosition, yearPos = _formatInfo.YearPosition,
                    hourPos = _formatInfo.HourPosition.Value, minutePos = _formatInfo.MinutePosition.Value, secondPos = _formatInfo.SecondPosition ?? 5,
                    amPmPos = _formatInfo.AmPmPosition ?? 6;
                FixNumericTextBoxesPositions(ref dayPos, ref monthPos, ref yearPos, ref hourPos, ref minutePos, ref secondPos, ref amPmPos);

                _textBoxes[dayPos]    = NumericTBDay;
                _textBoxes[monthPos]  = NumericTBMonth;
                _textBoxes[yearPos]   = NumericTBYear;
                _textBoxes[hourPos]   = NumericTBHour;
                _textBoxes[minutePos] = NumericTBMinute;
                _textBoxes[secondPos] = NumericTBSecond;
                _textBoxes[amPmPos]   = ListTBAmPm;

                // Fix tab order inside control.
                NumericTBDay.SetCurrentValue(TabIndexProperty, dayPos);
                NumericTBMonth.SetCurrentValue(TabIndexProperty, monthPos);
                NumericTBYear.SetCurrentValue(TabIndexProperty, yearPos);
                NumericTBHour.SetCurrentValue(TabIndexProperty, hourPos);
                NumericTBMinute.SetCurrentValue(TabIndexProperty, minutePos);
                NumericTBSecond.SetCurrentValue(TabIndexProperty, secondPos);
                ListTBAmPm.SetCurrentValue(TabIndexProperty, amPmPos);
                DatePickerIcon.SetCurrentValue(TabIndexProperty, Int32.MaxValue);

                SubscribeNumericTextBoxes();

                Separator1.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator1);
                Separator2.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator2);
                Separator3.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator3);
                Separator4.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator4);
                Separator5.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator5);
                Separator6.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator6);
                Separator7.SetCurrentValue(TextBlock.TextProperty, Value == null ? string.Empty : _formatInfo.Separator7);
            }
            finally
            {
                _applyingFormat = false;
            }
        }