17

我想更改 WPF 应用程序中 DateTimePicker 中选择的日期格式

4

9 回答 9

15

我最近正在处理这个问题。我找到了一种执行这种自定义格式的简单方法,希望对您有所帮助。您需要做的第一件事是在您的 XAML 中将特定样式应用于您当前的 DatePicker,就像这样:

<DatePicker.Resources>
    <Style TargetType="{x:Type DatePickerTextBox}">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <TextBox x:Name="PART_TextBox" Width="113" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Text="{Binding Path=SelectedDate,Converter={StaticResource DateTimeFormatter},RelativeSource={RelativeSource AncestorType={x:Type DatePicker}},ConverterParameter=dd-MMM-yyyy}" BorderBrush="{DynamicResource BaseBorderBrush}" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DatePicker.Resources>

正如您在这部分中所注意到的,当时存在一个名为 DateTimeFormatter 的转换器,以绑定到“PART_TextBox”的 Text 属性。此转换器接收包含您的自定义格式的转换器参数。最后,我们在 C# 中为 DateTimeFormatter 转换器添加代码。

public class DateTimeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        DateTime? selectedDate = value as DateTime?;

        if (selectedDate != null)
        {
            string dateTimeFormat = parameter as string;
            return selectedDate.Value.ToString(dateTimeFormat);
        }

        return "Select Date";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {

            var valor = value as string;
            if (!string.IsNullOrEmpty(valor))
            {
                var retorno = DateTime.Parse(valor);
                return retorno;
            }

            return null;
        }
        catch
        {
            return DependencyProperty.UnsetValue;
        }
    }
}

我希望这对你有帮助。请让我知道任何问题或改进建议。

于 2010-12-09T23:22:54.380 回答
5
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";      
于 2010-12-07T11:07:26.103 回答
4

将此样式添加到您的 xaml 或 App.xaml 文件

 <Style TargetType="{x:Type DatePickerTextBox}">
     <Setter Property="VerticalContentAlignment" Value="Center"/>
     <Setter Property="Control.Template">
         <Setter.Value>
             <ControlTemplate>
                 <TextBox x:Name="PART_TextBox"
            Text="{Binding Path=SelectedDate, StringFormat='dd.MM.yyyy', 
            RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
             </ControlTemplate>
         </Setter.Value>
     </Setter>
 </Style>
于 2016-04-05T09:32:29.863 回答
3

在 XAML 中:

<toolkit:DatePicker SelectedDateFormat="Long" />

或者

<toolkit:DatePicker SelectedDateFormat="Short" />
于 2009-11-10T05:54:11.997 回答
2

感谢@Fernando García 提供的基础。

我为 DatePicker 编写了一个 DateFormat 附加属性,它允许您提供一个格式字符串用于显示和输入。

对于输入,它将尝试使用提供的格式进行解析,然后回退到尝试使用当前区域性的格式进行解析。

问题格式的示例用法:

<DatePicker my:DatePickerDateFormat.DateFormat="dd/MMM/yyyy"/>

DateFormat 附加属性是:

public class DatePickerDateFormat
{
    public static readonly DependencyProperty DateFormatProperty =
        DependencyProperty.RegisterAttached("DateFormat", typeof (string), typeof (DatePickerDateFormat),
                                            new PropertyMetadata(OnDateFormatChanged));

    public static string GetDateFormat(DependencyObject dobj)
    {
        return (string) dobj.GetValue(DateFormatProperty);
    }

    public static void SetDateFormat(DependencyObject dobj, string value)
    {
        dobj.SetValue(DateFormatProperty, value);
    }

    private static void OnDateFormatChanged(DependencyObject dobj, DependencyPropertyChangedEventArgs e)
    {
        var datePicker = (DatePicker) dobj;

        Application.Current.Dispatcher.BeginInvoke(
            DispatcherPriority.Loaded, new Action<DatePicker>(ApplyDateFormat), datePicker);
    }

    private static void ApplyDateFormat(DatePicker datePicker)
    {
        var binding = new Binding("SelectedDate")
            {
                RelativeSource = new RelativeSource {AncestorType = typeof (DatePicker)},
                Converter = new DatePickerDateTimeConverter(),
                ConverterParameter = new Tuple<DatePicker, string>(datePicker, GetDateFormat(datePicker))
            };
        var textBox = GetTemplateTextBox(datePicker);
        textBox.SetBinding(TextBox.TextProperty, binding);

        textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown;
        textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown;

        datePicker.CalendarOpened -= DatePickerOnCalendarOpened;
        datePicker.CalendarOpened += DatePickerOnCalendarOpened;
    }

    private static TextBox GetTemplateTextBox(Control control)
    {
        control.ApplyTemplate();
        return (TextBox) control.Template.FindName("PART_TextBox", control);
    }

    private static void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key != Key.Return)
            return;

        /* DatePicker subscribes to its TextBox's KeyDown event to set its SelectedDate if Key.Return was
         * pressed. When this happens its text will be the result of its internal date parsing until it
         * loses focus or another date is selected. A workaround is to stop the KeyDown event bubbling up
         * and handling setting the DatePicker.SelectedDate. */

        e.Handled = true;

        var textBox = (TextBox) sender;
        var datePicker = (DatePicker) textBox.TemplatedParent;
        var dateStr = textBox.Text;
        var formatStr = GetDateFormat(datePicker);
        datePicker.SelectedDate = DatePickerDateTimeConverter.StringToDateTime(datePicker, formatStr, dateStr);
    }

    private static void DatePickerOnCalendarOpened(object sender, RoutedEventArgs e)
    {
        /* When DatePicker's TextBox is not focused and its Calendar is opened by clicking its calendar button
         * its text will be the result of its internal date parsing until its TextBox is focused and another
         * date is selected. A workaround is to set this string when it is opened. */

        var datePicker = (DatePicker) sender;
        var textBox = GetTemplateTextBox(datePicker);
        var formatStr = GetDateFormat(datePicker);
        textBox.Text = DatePickerDateTimeConverter.DateTimeToString(formatStr, datePicker.SelectedDate);
    }

    private class DatePickerDateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var formatStr = ((Tuple<DatePicker, string>) parameter).Item2;
            var selectedDate = (DateTime?) value;
            return DateTimeToString(formatStr, selectedDate);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var tupleParam = ((Tuple<DatePicker, string>) parameter);
            var dateStr = (string) value;
            return StringToDateTime(tupleParam.Item1, tupleParam.Item2, dateStr);
        }

        public static string DateTimeToString(string formatStr, DateTime? selectedDate)
        {
            return selectedDate.HasValue ? selectedDate.Value.ToString(formatStr) : null;
        }

        public static DateTime? StringToDateTime(DatePicker datePicker, string formatStr, string dateStr)
        {
            DateTime date;
            var canParse = DateTime.TryParseExact(dateStr, formatStr, CultureInfo.CurrentCulture,
                                                  DateTimeStyles.None, out date);

            if (!canParse)
                canParse = DateTime.TryParse(dateStr, CultureInfo.CurrentCulture, DateTimeStyles.None, out date);

            return canParse ? date : datePicker.SelectedDate;
        }
    }
}
于 2012-10-20T22:28:37.010 回答
1
DatePicker1.SelectedDate = DatePicker1.SelectedDate.Value.ToString("dd/MM/yyyy")
于 2009-12-29T10:05:59.613 回答
0

对我来说,改变环境来改变DatePicker格式(比如Thread.CurrentCulture)不是一个好主意。当然,您可以创建Control派生DatePicker并实现依赖属性,例如Format,但这需要花费太多精力。

我发现的简单而优雅的解决方案是将值绑定到SelectedDate自身,而不是绑定到一些未使用的属性(我ToolTip为此使用了属性),并在 SelectedDate 更改时更新此属性。

单向绑定的 C# 实现如下所示:

    DatePicker datePicker = new DatePicker();
    datePicker.SetBinding(ToolTipProperty, "Date");
    datePicker.SelectedDateChanged += (s, ea) =>
        {
            DateTime? date = datePicker.SelectedDate;
            string value = date != null ? date.Value.ToString("yyyy-MM-dd") : null;
            datePicker.ToolTip = value;
        };

XAML+C# 应如下所示:

XAML:

<DatePicker ToolTip="{Binding Date Mode=TwoWay}"
            SelectedDateChanged="DatePicker_SelectedDateChanged"/>

C#:

private void DatePicker_SelectedDateChanged(object sender, EventArgs ea)
{
    DatePicker datePicker = (DatePicker)sender;
    DateTime? date = datePicker.SelectedDate;
    string value = date != null ? date.Value.ToString("yyyy-MM-dd") : null;
    datePicker.ToolTip = value;
}

对于两种方式的实现处理ToolTipChanged事件更新的方式相同SelectedDate

于 2011-11-11T14:20:01.620 回答
0

通常,日期时间格式存储在资源文件中,因为这将有助于应用程序的国际化。

您可以从资源文件中选择格式并使用ToString(DATE_FORMAT)

在您的情况下,您可能想要使用

dateTimePicker.SelectedDate.ToString("dd-MMM-yyyy");
于 2009-03-13T05:47:53.200 回答
0

尝试这个

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
      DateLabel.Content = Convert.ToDateTime(datePicker1.Text).ToString("dd-MM-yyyy");
    }
于 2016-10-29T07:50:07.643 回答