文章分類/Infragistics
我們將介紹如何自定義XamDateTimeEditor中顯示的日曆中特定日期的背景顏色和文本顏色。
實施方法
XamDateTimeEditor 的日曆部分中的日期控件由 CalendarDay 控件表示。
設置 CalendarDay 背景顏色 (Background) 和前景色 (Foreground) 以自定義週六和周日的表示方式。
此外,要指定特定日期,請在 ConverterParameter 屬性中設置以逗號分隔的日期信息。
另外,轉換器在特定日期做出判斷。在下面的示例中,更改了特定日期(週六和周日)的背景顏色。
/// /// public class HolidayToBackgroundConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (parameter != null && !string.IsNullOrEmpty(parameter.ToString())) { string[] holidays = parameter.ToString().Split(','); foreach(string holidayStr in holidays) { DateTime holiday = DateTime.Parse(holidayStr); if((value as CalendarDay).StartDate.Date.ToString("yyyy/MM/dd") == holiday.ToString("yyyy/MM/dd")) { return Brushes.Orange; } } } if ((value as CalendarDay).StartDate.DayOfWeek == DayOfWeek.Saturday) { return Brushes.Blue; } else if ((value as CalendarDay).StartDate.DayOfWeek == DayOfWeek.Sunday) { return Brushes.Red; } return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
還為文本顏色創建轉換器。
/// /// カレンダー日付から特定日付の文字色を変換するコンバーター /// public class HolidayToForegroundConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { // 特定日付の情報を取得 if (parameter != null && !string.IsNullOrEmpty(parameter.ToString())) { string[] holidays = parameter.ToString().Split(','); foreach (string holidayStr in holidays) { DateTime holiday = DateTime.Parse(holidayStr); if ((value as CalendarDay).StartDate.Date.ToString("yyyy/MM/dd") == holiday.ToString("yyyy/MM/dd")) { return Brushes.White; } } } // 土曜 if ((value as CalendarDay).StartDate.DayOfWeek == DayOfWeek.Saturday) { return Brushes.White; } // 日曜 else if ((value as CalendarDay).StartDate.DayOfWeek == DayOfWeek.Sunday) { return Brushes.White; } return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
另外,當選擇上個月或下個月的日曆時,插入以下過程以刷新繪圖。
private void XamMonthCalendar_ExecutedCommand(object sender, Infragistics.Windows.Controls.Events.ExecutedCommandEventArgs e) { XamMonthCalendar xamMonthCalendar = sender as XamMonthCalendar; xamMonthCalendar.Dispatcher.BeginInvoke(new Action(() => { DependencyObject fobj = Infragistics.Windows.Utilities.GetDescendantFromType( xamMonthCalendar, true, (calDay) => { BindingExpression bg = calDay.GetBindingExpression(CalendarDay.BackgroundProperty); if (bg != null) { bg.UpdateTarget(); } BindingExpression fg = calDay.GetBindingExpression(CalendarDay.ForegroundProperty); if (fg != null) { fg.UpdateTarget(); } return false; }); }),DispatcherPriority.Background); }