1、獲得當前顯示的月份:ide
DateTime SelectedDay = this.MC.DisplayDate;this
2、獲得當前選中的天,獲得當前選中的周,獲得當前顯示的月份:spa
若是你使用系統默認的事件SelectedDateChanged是很難獲取焦點的,給Calendar註冊MC_MouseLeftButtonUp事件設計
1 //在初始化時註冊事件
2 MC.AddHandler(Button.MouseLeftButtonDownEvent, new RoutedEventHandler(MC_MouseLeftButtonUp), true); 3
4 private void MC_MouseLeftButtonUp(object sender, RoutedEventArgs e)
事件的關鍵代碼:code
1 if (sender is Calendar) 2 { 3 if (MC.InputHitTest(Mouse.GetPosition(e.Source as FrameworkElement)) is TextBlock) 4 { 5 TextBlock tb = MC.InputHitTest(Mouse.GetPosition(e.Source as FrameworkElement)) 6 as TextBlock;//本行代碼是個關鍵,使用了WPF內置的碰撞檢測 7 if (tb != null) 8 { 9
10 try
11 { 12 //獲取選擇的是哪一天
13 int.Parse(tb.Text); 14 } 15
16 catch (Exception ex) //Click The WeekDaysButton
17 { 18
19 //獲取選擇的是星期幾,若是點擊「週一、週二、...周7」按鈕,程序會走到此處來得出星期幾。
20 int indexofWeek = (tb.Parent as Grid).Children.IndexOf(tb); 21 //次方法能獲取切換月份按鈕後的當前月份
22 DateTime SelectedDay = this.MC.DisplayDate; 23 } 24 } 25 } 26 }
3、自定義日期的背景顏色對象
3.一、首先要自定義幾個類,用於模板轉換blog
1 public class CustemItems 2 { 3 bool isSpecific; 4
5 public bool IsSpecific 6 { 7 get { return isSpecific; } 8 set { isSpecific = value; } 9 } 10
11 DateTime d; 12
13 public DateTime Dete 14 { 15 get { return d; } 16 set { d = value; } 17 } 18 string s; 19
20 public string StrOfColor 21 { 22 get { return s; } 23 set { s = value; } 24 } 25 public CustemItems(DateTime d, string str) 26 { 27 Dete = d; 28 StrOfColor = str; 29 } 30 }
1 public class BlueLetterDayConverter : IValueConverter 2 { 3 public static List<CustemItems> dict = new List<CustemItems>(); 4
5
6 static BlueLetterDayConverter() 7 { 8
9 } 10
11 public object Convert(object value, Type targetType, 12 object parameter, CultureInfo culture) 13 { 14 string text = null; 15 for (int i = 0; i < dict.Count; i++) 16 { 17 if (dict[i].Dete == (DateTime)value) 18 { 19 text = dict[i].StrOfColor; 20 } 21 } 22
23 return text; 24 } 25
26 public object ConvertBack(object value, Type targetType, 27 object parameter, CultureInfo culture) 28 { 29 return null; 30 } 31 public void Add(DateTime date, string str) 32 { 33 dict.Add(new CustemItems(date, str)); 34 } 35
36 public static void Update(List<CustemItems> MIList) 37 { 38 dict.Clear(); 39 dict = MIList; 40 } 41
42 }
public class RedLetterDayConverter : IValueConverter { public static List<CustemItems> dict = new List<CustemItems>(); static RedLetterDayConverter() { } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string text = null; for (int i = 0; i < dict.Count; i++) { if (dict[i].Dete == (DateTime)value) { text = dict[i].StrOfColor; } } return text; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } public void Add(DateTime date, string str) { dict.Add(new CustemItems(date, str)); } public static void Update(List<CustemItems> MIList) { dict.Clear(); dict = MIList; } }
3.2 Calendar模板的定義事件
1 <Calendar x:Name="MC" HorizontalAlignment="Left" VerticalAlignment="Top"
2 MouseLeftButtonDown="MC_MouseLeftButtonUp"
3 >
4 <Calendar.CalendarDayButtonStyle>
5 <Style TargetType="{x:Type CalendarDayButton}">
6 <Setter Property="Template">
7 <Setter.Value>
8 <ControlTemplate TargetType="{x:Type CalendarDayButton}" x:Name="CalendarDayButtonControlTemplate">
9 <ControlTemplate.Resources>
10 <src:RedLetterDayConverter x:Key="convRed" />
11 <src:BlueLetterDayConverter x:Key="convBlue" />
12 </ControlTemplate.Resources>
13 <Grid>
14 <Rectangle x:Name="RedLetterDayBackground" IsHitTestVisible="False" Fill="Red"/>
15 <Rectangle x:Name="BlueLetterDayConverter" IsHitTestVisible="False" Fill="Blue"/>
16
17 <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
18 VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Margin="5,1,5,1"/>
19 </Grid>
20 <ControlTemplate.Triggers>
21 <DataTrigger Binding="{Binding Converter={StaticResource convRed}}" Value="{x:Null}">
22 <Setter TargetName="RedLetterDayBackground" Property="Visibility" Value="Hidden" />
23 </DataTrigger>
24 <DataTrigger Binding="{Binding Converter={StaticResource convBlue}}" Value="{x:Null}">
25 <Setter TargetName="BlueLetterDayConverter" Property="Visibility" Value="Hidden" />
26 </DataTrigger>
27 </ControlTemplate.Triggers>
28 </ControlTemplate>
29 </Setter.Value>
30 </Setter>
31 </Style>
32 </Calendar.CalendarDayButtonStyle>
33
34 </Calendar>
3.3 更新轉換模板的Listci
1 List<CustemItems> ItemListRed = new List<CustemItems>(); 2 List<CustemItems> ItemListBule = new List<CustemItems>(); 3 4 ... 5 6 RedLetterDayConverter.Update(ItemListRed); 7 BlueLetterDayConverter.Update(ItemListBule); 8 if (!StartTimer) 9 { 10 dispatcherTimer.Start(); 11 }
3.4 重點:資源
由於從根本上講ControlTemplate.Resources是一個靜態的資源,它沒法作到資源變動後,系統自動會通知控件(或者控件模板)去更新對應的UI顯示,這個在《WPF程序設計指南[Charles Petzold]》書中的第535頁至539頁有詳細介紹。因此咱們要在變動RedLetterDayConverter 或者BlueLetterDayConverter裏面的List<>對象之後,須要使用一個Timer去手動更新UI,從而觸發模板的更新。
1 void dispatcherTimer_Tick(object sender, EventArgs e) 2 { 3 StartTimer = true; 4 MC.DisplayDate = MC.DisplayDate.AddMonths(1); 5 MC.DisplayDate = MC.DisplayDate.AddMonths(-1); 6 Thread.Sleep(50); 7 dispatcherTimer.Stop(); 8 StartTimer = false; 9 }
完整的例子: