文章分類/

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

248 瀏覽人次
2023-07-19 更新

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

快速跳轉目錄

✦ 群昱 AccessSoft 你的全面軟體解決方案 ✦

身為全球眾多知名軟體在台灣合作夥伴,歡迎諮詢你需要的軟體服務!

Picture of 軟體專家
軟體專家

群昱作為全球知名軟體推薦合作夥伴,致力於提供更多軟體解決方案給你!

更多軟體新知

立即詢價

請留下完整資訊,以便我們提供精確的服務內容給你。

詢價資訊