Skip to content

Commit

Permalink
Limiting issue where the displayed date (#1594)
Browse files Browse the repository at this point in the history
Forcing the date to be withing the calendars min/max
  • Loading branch information
Keboo authored Jan 7, 2020
1 parent 7e83773 commit 25a2b7a
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion MaterialDesignThemes.Wpf/MaterialDateDisplay.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand All @@ -19,7 +20,25 @@ public MaterialDateDisplay()
}

public static readonly DependencyProperty DisplayDateProperty = DependencyProperty.Register(
nameof(DisplayDate), typeof(DateTime), typeof(MaterialDateDisplay), new PropertyMetadata(default(DateTime), DisplayDatePropertyChangedCallback));
nameof(DisplayDate), typeof(DateTime), typeof(MaterialDateDisplay), new PropertyMetadata(default(DateTime), DisplayDatePropertyChangedCallback, DisplayDateCoerceValue));

private static object DisplayDateCoerceValue(DependencyObject d, object baseValue)
{
if (d is FrameworkElement element &&
element.Language.GetSpecificCulture() is CultureInfo culture &&
baseValue is DateTime displayDate)
{
if (displayDate < culture.Calendar.MinSupportedDateTime)
{
return culture.Calendar.MinSupportedDateTime;
}
if (displayDate > culture.Calendar.MaxSupportedDateTime)
{
return culture.Calendar.MaxSupportedDateTime;
}
}
return baseValue;
}

private static void DisplayDatePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
Expand Down

0 comments on commit 25a2b7a

Please sign in to comment.