1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MaterialDesignThemes.Wpf
{
public class MaterialDateDisplay : Control
{
static MaterialDateDisplay()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MaterialDateDisplay), new FrameworkPropertyMetadata(typeof(MaterialDateDisplay)));
}
public MaterialDateDisplay()
{
SetCurrentValue(DisplayDateProperty, DateTime.Now.Date);
}
public static readonly DependencyProperty DisplayDateProperty = DependencyProperty.Register(
nameof(DisplayDate), typeof (DateTime), typeof (MaterialDateDisplay), new PropertyMetadata(default(DateTime), DisplayDatePropertyChangedCallback));
private static void DisplayDatePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
((MaterialDateDisplay)dependencyObject).UpdateComponents();
}
public DateTime DisplayDate
{
get { return (DateTime) GetValue(DisplayDateProperty); }
set { SetValue(DisplayDateProperty, value); }
}
private static readonly DependencyPropertyKey ComponentOneContentPropertyKey =
DependencyProperty.RegisterReadOnly(
"ComponentOneContent", typeof (string), typeof (MaterialDateDisplay),
new PropertyMetadata(default(string)));
public static readonly DependencyProperty ComponentOneContentProperty =
ComponentOneContentPropertyKey.DependencyProperty;
public string ComponentOneContent
{
get { return (string) GetValue(ComponentOneContentProperty); }
private set { SetValue(ComponentOneContentPropertyKey, value); }
}
private static readonly DependencyPropertyKey ComponentTwoContentPropertyKey =
DependencyProperty.RegisterReadOnly(
"ComponentTwoContent", typeof (string), typeof (MaterialDateDisplay),
new PropertyMetadata(default(string)));
public static readonly DependencyProperty ComponentTwoContentProperty =
ComponentTwoContentPropertyKey.DependencyProperty;
public string ComponentTwoContent
{
get { return (string) GetValue(ComponentTwoContentProperty); }
private set { SetValue(ComponentTwoContentPropertyKey, value); }
}
private static readonly DependencyPropertyKey ComponentThreeContentPropertyKey =
DependencyProperty.RegisterReadOnly(
"ComponentThreeContent", typeof (string), typeof (MaterialDateDisplay),
new PropertyMetadata(default(string)));
public static readonly DependencyProperty ComponentThreeContentProperty =
ComponentThreeContentPropertyKey.DependencyProperty;
public string ComponentThreeContent
{
get { return (string) GetValue(ComponentThreeContentProperty); }
private set { SetValue(ComponentThreeContentPropertyKey, value); }
}
private static readonly DependencyPropertyKey IsDayInFirstComponentPropertyKey =
DependencyProperty.RegisterReadOnly(
"IsDayInFirstComponent", typeof (bool), typeof (MaterialDateDisplay),
new PropertyMetadata(default(bool)));
public static readonly DependencyProperty IsDayInFirstComponentProperty =
IsDayInFirstComponentPropertyKey.DependencyProperty;
public bool IsDayInFirstComponent
{
get { return (bool) GetValue(IsDayInFirstComponentProperty); }
private set { SetValue(IsDayInFirstComponentPropertyKey, value); }
}
//FrameworkElement.LanguageProperty.OverrideMetadata(typeof (Calendar), (PropertyMetadata) new FrameworkPropertyMetadata(new PropertyChangedCallback(Calendar.OnLanguageChanged)));
private void UpdateComponents()
{
var culture = Language.GetSpecificCulture();
var dateTimeFormatInfo = culture.GetDateFormat();
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
ComponentTwoContent = DisplayDate.ToString("ddd,", culture).ToTitleCase(culture); // Day of week first
ComponentThreeContent = DisplayDate.ToString("yyyy", culture).ToTitleCase(culture); // Year always top
}
/// <summary>
/// Ripped straight from .Net FX.
/// </summary>
/// <param name="culture"></param>
/// <returns></returns>
internal static DateTimeFormatInfo GetDateFormat(CultureInfo culture)
{
if (culture.Calendar is GregorianCalendar)
{
return culture.DateTimeFormat;
}
else
{
GregorianCalendar foundCal = null;
DateTimeFormatInfo dtfi = null;
foreach (System.Globalization.Calendar cal in culture.OptionalCalendars)
{
if (cal is GregorianCalendar)
{
// Return the first Gregorian calendar with CalendarType == Localized
// Otherwise return the first Gregorian calendar
if (foundCal == null)
{
foundCal = cal as GregorianCalendar;
}
if (((GregorianCalendar)cal).CalendarType == GregorianCalendarTypes.Localized)
{
foundCal = cal as GregorianCalendar;
break;
}
}
}
if (foundCal == null)
{
// if there are no GregorianCalendars in the OptionalCalendars list, use the invariant dtfi
dtfi = ((CultureInfo)CultureInfo.InvariantCulture.Clone()).DateTimeFormat;
dtfi.Calendar = new GregorianCalendar();
}
else
{
dtfi = ((CultureInfo)culture.Clone()).DateTimeFormat;
dtfi.Calendar = foundCal;
}
return dtfi;
}
}
}
}
|