Infragistics|Ultimate UI for WPF|XamDateTimeEditor 設置特定日期的文本顏色/背景顏色

infragistics log



我們將介紹如何自定義XamDateTimeEditor中顯示的日曆中特定日期的背景顏色和文本顏色。


實施方法

XamDateTimeEditor 的日曆部分中的日期控件由 CalendarDay 控件表示。

設置 CalendarDay 背景顏色 (Background) 和前景色 (Foreground) 以自定義週六和周日的表示方式。

此外,要指定特定日期,請在 ConverterParameter 屬性中設置以逗號分隔的日期信息。

<igEditors:XamDateTimeEditor Margin="10" >
<igEditors:XamDateTimeEditor.Resources>
<Style TargetType="{x:Type igEditors:XamMonthCalendar}">
<EventSetter Event="ExecutedCommand" Handler="XamMonthCalendar_ExecutedCommand"/>
</Style>
<Style TargetType="{x:Type igEditors:CalendarDay}">
<!-- 特定日の背景色を変更するコンバーター -->
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource HolidayToBackgroundConverter},
ConverterParameter='2019/10/14,2019/10/22'}" />
<!-- 特定日の文字色を変更するコンバーター -->
<Setter Property="Foreground"
Value="{Binding RelativeSource={RelativeSource Self},
Converter={StaticResource HolidayToForegroundConverter},
ConverterParameter='2019/10/14,2019/10/22'}" />
</Style>
</igEditors:XamDateTimeEditor.Resources>
</igEditors:XamDateTimeEditor>


另外,轉換器在特定日期做出判斷。在下面的示例中,更改了特定日期(週六和周日)的背景顏色。

  /// 

    /// 
    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();
        }
    }

另外,當選擇上個月或下個月的日曆時,插入以下過程以刷新繪圖。

<Style TargetType="{x:Type igEditors:XamMonthCalendar}">
<EventSetter Event="ExecutedCommand" Handler="XamMonthCalendar_ExecutedCommand"/>
</Style>


  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);
  
  }  



延伸閱讀
aa71435723的大頭照
Winston

Eggplant DAI 自動化測試專家。

留言