1
1
using System ;
2
- using System . Globalization ;
2
+ using System . Linq ;
3
3
using System . Windows ;
4
4
using System . Windows . Controls ;
5
+ using Calendar = System . Windows . Controls . Calendar ;
5
6
6
7
namespace MaterialDesignThemes . Wpf
7
- {
8
+ {
8
9
public class MaterialDateDisplay : Control
9
10
{
10
11
static MaterialDateDisplay ( )
@@ -14,11 +15,11 @@ static MaterialDateDisplay()
14
15
15
16
public MaterialDateDisplay ( )
16
17
{
17
- SetCurrentValue ( DisplayDateProperty , DateTime . Now . Date ) ;
18
+ SetCurrentValue ( DisplayDateProperty , DateTime . Today ) ;
18
19
}
19
20
20
21
public static readonly DependencyProperty DisplayDateProperty = DependencyProperty . Register (
21
- nameof ( DisplayDate ) , typeof ( DateTime ) , typeof ( MaterialDateDisplay ) , new PropertyMetadata ( default ( DateTime ) , DisplayDatePropertyChangedCallback ) ) ;
22
+ nameof ( DisplayDate ) , typeof ( DateTime ) , typeof ( MaterialDateDisplay ) , new PropertyMetadata ( default ( DateTime ) , DisplayDatePropertyChangedCallback ) ) ;
22
23
23
24
private static void DisplayDatePropertyChangedCallback ( DependencyObject dependencyObject , DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs )
24
25
{
@@ -27,127 +28,102 @@ private static void DisplayDatePropertyChangedCallback(DependencyObject dependen
27
28
28
29
public DateTime DisplayDate
29
30
{
30
- get { return ( DateTime ) GetValue ( DisplayDateProperty ) ; }
31
+ get { return ( DateTime ) GetValue ( DisplayDateProperty ) ; }
31
32
set { SetValue ( DisplayDateProperty , value ) ; }
32
33
}
33
34
34
35
private static readonly DependencyPropertyKey ComponentOneContentPropertyKey =
35
36
DependencyProperty . RegisterReadOnly (
36
- " ComponentOneContent" , typeof ( string ) , typeof ( MaterialDateDisplay ) ,
37
+ nameof ( ComponentOneContent ) , typeof ( string ) , typeof ( MaterialDateDisplay ) ,
37
38
new PropertyMetadata ( default ( string ) ) ) ;
38
39
39
40
public static readonly DependencyProperty ComponentOneContentProperty =
40
41
ComponentOneContentPropertyKey . DependencyProperty ;
41
42
42
43
public string ComponentOneContent
43
44
{
44
- get { return ( string ) GetValue ( ComponentOneContentProperty ) ; }
45
+ get { return ( string ) GetValue ( ComponentOneContentProperty ) ; }
45
46
private set { SetValue ( ComponentOneContentPropertyKey , value ) ; }
46
47
}
47
48
48
49
private static readonly DependencyPropertyKey ComponentTwoContentPropertyKey =
49
50
DependencyProperty . RegisterReadOnly (
50
- " ComponentTwoContent" , typeof ( string ) , typeof ( MaterialDateDisplay ) ,
51
+ nameof ( ComponentTwoContent ) , typeof ( string ) , typeof ( MaterialDateDisplay ) ,
51
52
new PropertyMetadata ( default ( string ) ) ) ;
52
53
53
54
public static readonly DependencyProperty ComponentTwoContentProperty =
54
55
ComponentTwoContentPropertyKey . DependencyProperty ;
55
56
56
57
public string ComponentTwoContent
57
58
{
58
- get { return ( string ) GetValue ( ComponentTwoContentProperty ) ; }
59
+ get { return ( string ) GetValue ( ComponentTwoContentProperty ) ; }
59
60
private set { SetValue ( ComponentTwoContentPropertyKey , value ) ; }
60
61
}
61
62
62
63
private static readonly DependencyPropertyKey ComponentThreeContentPropertyKey =
63
64
DependencyProperty . RegisterReadOnly (
64
- " ComponentThreeContent" , typeof ( string ) , typeof ( MaterialDateDisplay ) ,
65
+ nameof ( ComponentThreeContent ) , typeof ( string ) , typeof ( MaterialDateDisplay ) ,
65
66
new PropertyMetadata ( default ( string ) ) ) ;
66
67
67
68
public static readonly DependencyProperty ComponentThreeContentProperty =
68
69
ComponentThreeContentPropertyKey . DependencyProperty ;
69
70
70
71
public string ComponentThreeContent
71
72
{
72
- get { return ( string ) GetValue ( ComponentThreeContentProperty ) ; }
73
+ get { return ( string ) GetValue ( ComponentThreeContentProperty ) ; }
73
74
private set { SetValue ( ComponentThreeContentPropertyKey , value ) ; }
74
75
}
75
76
76
- private static readonly DependencyPropertyKey IsDayInFirstComponentPropertyKey =
77
- DependencyProperty . RegisterReadOnly (
78
- " IsDayInFirstComponent" , typeof ( bool ) , typeof ( MaterialDateDisplay ) ,
79
- new PropertyMetadata ( default ( bool ) ) ) ;
77
+ private static readonly DependencyPropertyKey IsDayInFirstComponentPropertyKey =
78
+ DependencyProperty . RegisterReadOnly (
79
+ nameof ( IsDayInFirstComponent ) , typeof ( bool ) , typeof ( MaterialDateDisplay ) ,
80
+ new PropertyMetadata ( default ( bool ) ) ) ;
80
81
81
- public static readonly DependencyProperty IsDayInFirstComponentProperty =
82
- IsDayInFirstComponentPropertyKey . DependencyProperty ;
82
+ public static readonly DependencyProperty IsDayInFirstComponentProperty =
83
+ IsDayInFirstComponentPropertyKey . DependencyProperty ;
83
84
84
- public bool IsDayInFirstComponent
85
- {
86
- get { return ( bool ) GetValue ( IsDayInFirstComponentProperty ) ; }
87
- private set { SetValue ( IsDayInFirstComponentPropertyKey , value ) ; }
88
- }
85
+ public bool IsDayInFirstComponent
86
+ {
87
+ get { return ( bool ) GetValue ( IsDayInFirstComponentProperty ) ; }
88
+ private set { SetValue ( IsDayInFirstComponentPropertyKey , value ) ; }
89
+ }
89
90
90
- //FrameworkElement.LanguageProperty.OverrideMetadata(typeof (Calendar), (PropertyMetadata) new FrameworkPropertyMetadata(new PropertyChangedCallback(Calendar.OnLanguageChanged)));
91
91
private void UpdateComponents ( )
92
92
{
93
93
var culture = Language . GetSpecificCulture ( ) ;
94
94
var dateTimeFormatInfo = culture . GetDateFormat ( ) ;
95
+ var minDateTime = dateTimeFormatInfo . Calendar . MinSupportedDateTime ;
96
+ var maxDateTime = dateTimeFormatInfo . Calendar . MaxSupportedDateTime ;
95
97
96
- ComponentOneContent = DisplayDate . ToString ( dateTimeFormatInfo . MonthDayPattern . Replace ( "MMMM" , "MMM" ) , culture ) . ToTitleCase ( culture ) ; //Day Month folowing culture order. We don't want the month to take too much space
97
- ComponentTwoContent = DisplayDate . ToString ( "ddd," , culture ) . ToTitleCase ( culture ) ; // Day of week first
98
- ComponentThreeContent = DisplayDate . ToString ( "yyyy" , culture ) . ToTitleCase ( culture ) ; // Year always top
99
- }
100
-
101
- /// <summary>
102
- /// Ripped straight from .Net FX.
103
- /// </summary>
104
- /// <param name="culture"></param>
105
- /// <returns></returns>
106
- internal static DateTimeFormatInfo GetDateFormat ( CultureInfo culture )
107
- {
108
- if ( culture . Calendar is GregorianCalendar )
98
+ if ( DisplayDate < minDateTime )
109
99
{
110
- return culture . DateTimeFormat ;
100
+ SetDisplayDateOfCalendar ( minDateTime ) ;
101
+
102
+ // return to avoid second formatting of the same value
103
+ return ;
111
104
}
112
- else
105
+
106
+ if ( DisplayDate > maxDateTime )
113
107
{
114
- GregorianCalendar foundCal = null ;
115
- DateTimeFormatInfo dtfi = null ;
116
-
117
- foreach ( System . Globalization . Calendar cal in culture . OptionalCalendars )
118
- {
119
- if ( cal is GregorianCalendar )
120
- {
121
- // Return the first Gregorian calendar with CalendarType == Localized
122
- // Otherwise return the first Gregorian calendar
123
- if ( foundCal == null )
124
- {
125
- foundCal = cal as GregorianCalendar ;
126
- }
127
-
128
- if ( ( ( GregorianCalendar ) cal ) . CalendarType == GregorianCalendarTypes . Localized )
129
- {
130
- foundCal = cal as GregorianCalendar ;
131
- break ;
132
- }
133
- }
134
- }
135
-
136
- if ( foundCal == null )
137
- {
138
- // if there are no GregorianCalendars in the OptionalCalendars list, use the invariant dtfi
139
- dtfi = ( ( CultureInfo ) CultureInfo . InvariantCulture . Clone ( ) ) . DateTimeFormat ;
140
- dtfi . Calendar = new GregorianCalendar ( ) ;
141
- }
142
- else
143
- {
144
- dtfi = ( ( CultureInfo ) culture . Clone ( ) ) . DateTimeFormat ;
145
- dtfi . Calendar = foundCal ;
146
- }
147
-
148
- return dtfi ;
108
+ SetDisplayDateOfCalendar ( maxDateTime ) ;
109
+
110
+ // return to avoid second formatting of the same value
111
+ return ;
149
112
}
150
- }
151
113
114
+ ComponentOneContent = DisplayDate . ToString ( dateTimeFormatInfo . MonthDayPattern . Replace ( "MMMM" , "MMM" ) , culture ) . ToTitleCase ( culture ) ; //Day Month following culture order. We don't want the month to take too much space
115
+ ComponentTwoContent = DisplayDate . ToString ( "ddd," , culture ) . ToTitleCase ( culture ) ; // Day of week first
116
+ ComponentThreeContent = DisplayDate . ToString ( "yyyy" , culture ) . ToTitleCase ( culture ) ; // Year always top
117
+ }
118
+
119
+ private void SetDisplayDateOfCalendar ( DateTime displayDate )
120
+ {
121
+ Calendar calendarControl = this . GetVisualAncestry ( ) . OfType < Calendar > ( ) . FirstOrDefault ( ) ;
122
+
123
+ if ( calendarControl != null )
124
+ {
125
+ calendarControl . DisplayDate = displayDate ;
126
+ }
127
+ }
152
128
}
153
129
}
0 commit comments