2013-03-07 122 views
0

我想修改標準的WPF DatePicker以允許一週選擇。如果我將單個DateTime作爲SelectedDate返回,那很好,但是我需要UI在鼠標懸停時選擇整個星期的行。更改WPF DatePicker一次選擇一個星期

我已經繼承並附加到SelectedDateChanged,以便它將返回StartDayOfWeek作爲SelectedDate(代碼如下)。

我已經知道如何突出顯示整個星期(使用鼠標事件)。我希望有一個更簡單的方法。

 // force selection to be for first previous start DOW 
     this.SelectedDateChanged += (sender, args) => { 
      var newDate = (DateTime) args.AddedItems[0]; 

      while (newDate.DayOfWeek != FirstDayOfWeek) 
       newDate = newDate.AddDays(-1); 

      SelectedDate = newDate; 
     }; 

編輯

我說我目前的解決方案作爲一個答案。仍然在尋找一種突出範圍的好方法,而不是我現在正在做的。

回答

0

想我會加上我目前的解決方案,這是爲了防止任何不符合FirstDayOfWeek的日期,並確保如果他們超出我10年的範圍,無論如何都會選擇正確的日期。

public class LocalizedWeekPicker : LocalizedDatePicker 
{ 
    /// <summary> 
    /// An LocalizedDatePicker that disallows selecting any date that does not have the same DOW as the FirstDayOfWeek property. 
    /// It does this by disabling the dates N (currently=10) years forward and backward from today as well as automatically updating the date 
    /// if you choose something more than 10 years away from today. 
    /// </summary> 
    public LocalizedWeekPicker() : base() 
    { 
     this.Loaded += (s1, a1) => { 
      this.IsTodayHighlighted = false; 

      if (SelectedDate.HasValue) 
      { 
       var tempDate = new DateTime(SelectedDate.Value.Ticks); 
       SelectedDate = DateTimeUtils.GetPreviousDateOnDayOfWeek(FirstDayOfWeek, tempDate); 
      } 

      DisableDays(); 

      this.SelectedDateChanged += (s2, a2) => { 
       var tempDate = new DateTime(((DateTime)a2.AddedItems[0]).Ticks); 
       SelectedDate = DateTimeUtils.GetPreviousDateOnDayOfWeek(FirstDayOfWeek, tempDate); 
      }; 
     }; 
    } 

    // goes forward and back n years and disables dates that don't have the same start DOW as the FirstDayOfWeek 
    private void DisableDays() 
    { 
     int years = 10; 

     var current = DateTimeUtils.GetPreviousDateOnDayOfWeek(FirstDayOfWeek, DateTime.Now); 
     for (int weeks = 0; weeks < (52 * years); weeks++) 
     { 
      this.BlackoutDates.Add(new CalendarDateRange {Start = current.AddDays(1), End = current.AddDays(6)}); 
      current = current.AddDays(7); 
     } 

     current = DateTimeUtils.GetPreviousDateOnDayOfWeek(FirstDayOfWeek, DateTime.Now); 
     for (int weeks = 0; weeks < (52 * years); weeks++) 
     { 
      this.BlackoutDates.Add(new CalendarDateRange { Start = current.AddDays(1), End = current.AddDays(6) }); 
      current = current.AddDays(-7); 
     } 
    } 
} 
0

,你可以隨時使用組件藝術控件或其他第三方的控制,因爲他們有自定義日期控件每一個場景 - 我以前用過的部件藝術的控制,效果不錯

+0

是的,我意識到這一點,但它不是在這種情況下的選項。 – JoeB 2013-03-07 17:10:26

相關問題