2013-02-07 45 views
0

我已派生DatePicker類以支持額外的DateMode屬性,該屬性將允許用戶僅按照DateMode(Day,Month,Year)使用控件。因此,如果DateMode設置爲Year,則控件將無法進一步深入查看一年中的月份,然後再查看當月的幾天。 該控件運行良好,但有一個問題。儘管我已經在DatePicker模板的'PART_TextBox'控件上應用了字符串格式,它將根據DateMode更改格式,但DatePicker控件很快失去焦點時,格式將丟失。以下是我派生的DatePicker控制代碼:DatePicker SelectedDate字符串格式

public class MyDatePicker : DatePicker 
{ 
    public string DateMode 
    { 
     get { return (string)GetValue(DateModeProperty); } 
     set { SetValue(DateModeProperty, value); } 
    } 

    // Using a DependencyProperty as the backing store for DateMode. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty DateModeProperty = 
     DependencyProperty.Register("DateMode", typeof(string), typeof(MyDatePicker), new UIPropertyMetadata("Day")); 

    protected override void OnCalendarOpened(RoutedEventArgs e) 
    { 
     var popup = this.Template.FindName("PART_Popup", this) as Popup; 
     var tb = this.Template.FindName("PART_TextBox", this) as TextBox; 

     if (popup != null && popup.Child is System.Windows.Controls.Calendar) 
     { 
      if (DateMode == "Year") 
       ((System.Windows.Controls.Calendar)popup.Child).DisplayMode = CalendarMode.Decade; 
      else if (DateMode == "Month") 
       ((System.Windows.Controls.Calendar)popup.Child).DisplayMode = CalendarMode.Year; 
      else if (DateMode == "Day") 
       ((System.Windows.Controls.Calendar)popup.Child).DisplayMode = CalendarMode.Month; 
     } 
     ((System.Windows.Controls.Calendar)popup.Child).DisplayModeChanged += new EventHandler<CalendarModeChangedEventArgs>(DatePickerCo_DisplayModeChanged); 
    } 

    protected override void OnCalendarClosed(RoutedEventArgs e) 
    { 
     base.OnCalendarClosed(e); 
     IsDropDownOpen = false; 
     var popup = this.Template.FindName("PART_Popup", this) as Popup; 
     ((System.Windows.Controls.Calendar)popup.Child).DisplayModeChanged -= new EventHandler<CalendarModeChangedEventArgs>(DatePickerCo_DisplayModeChanged); 
    } 

    private void DatePickerCo_DisplayModeChanged(object sender, CalendarModeChangedEventArgs e) 
    { 
     var popup = this.Template.FindName("PART_Popup", this) as Popup; 
     var tb = this.Template.FindName("PART_TextBox", this) as TextBox; 

     if (popup != null && popup.Child is System.Windows.Controls.Calendar) 
     { 
      var _calendar = popup.Child as System.Windows.Controls.Calendar; 

      if (DateMode == "Month" && _calendar.DisplayMode == CalendarMode.Month) 
      { 
       if (IsDropDownOpen) 
       { 
        this.SelectedDate = _calendar.DisplayDate; 
        this.IsDropDownOpen = false; 
        _calendar.DisplayMode = CalendarMode.Year; 
       } 
       tb.Text = this.SelectedDate.Value.ToString("MMM yyyy"); 
      } 
      else if (DateMode == "Year" && _calendar.DisplayMode == CalendarMode.Year) 
      { 
       if (IsDropDownOpen) 
       { 
        this.SelectedDate = _calendar.DisplayDate; 
        this.IsDropDownOpen = false; 
        _calendar.DisplayMode = CalendarMode.Decade; 
       } 
       tb.Text = this.SelectedDate.Value.ToString("yyyy"); 
      } 
     } 
    } 
} 

回答

0

我想你可能會干擾基類。我不確定確切的設置,但我認爲PART_TextBox有一個綁定,它由DatePicker設置,可能在LostFocus上更新,並循環回去清除您直接設置的Text值。您可以嘗試使用BindingOperations.GetBinding來訪問Text上的綁定,並修改它的StringFormat而不是直接設置Text