DatePicker SelectedDate字符串格式

时间:2013-02-07 20:55:05

标签: wpf datepicker string-formatting

我派生了DatePicker类以支持额外的DateMode属性,该属性允许用户仅根据DateMode(Day,Month,Year)使用该控件。因此,如果将DateMode设置为Year,则控件将无法进一步向下钻取以查看一年中的几个月,然后进一步查看该月的几天。 控制工作正常,但是有一个问题。虽然我已经在DatePicker模板的'PART_TextBox'控件上应用了字符串格式,这将根据DateMode更改格式,但是一旦DatePicker控件失去焦点,格式就会丢失。以下是我派生的DatePicker控件代码:

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

1 个答案:

答案 0 :(得分:0)

我认为您可能会干扰基类。我不确定确切的设置,但我认为PART_TextBox有一个由DatePicker设置的绑定,可能会更新LostFocus并循环返回以清除{Text 1}}您直接设置的值。您可以尝试使用BindingOperations.GetBinding访问Text上的绑定并修改它的StringFormat,而不是直接设置Text