如何更改 WPF 应用程序中 DateTimePicker 的格式(例如 dd/MMM/yyyy)

发布于 2024-07-14 13:09:32 字数 45 浏览 10 评论 0原文

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

I want to Change the Format of date selected in DateTimePicker in WPF Application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

无所的.畏惧 2024-07-21 13:09:32

我最近正在处理这个问题。 我找到了一种执行此自定义格式的简单方法,希望对您有所帮助。 您需要做的第一件事就是在 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 的转换器,以绑定到“的 Text 属性” PART_文本框”。 该转换器接收包含您的自定义格式的转换器参数。 最后,我们为 DateTimeFormatter 转换器添加 C# 代码。

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

我希望这对你有帮助。 如有任何问题或改进建议,请告诉我。

I was handling with this issue rencetly. I found a simple way to perform this custom format and I hope that this help you. First thing that you need to do is apply a specific style to your current DatePicker just like this, in your XAML:

<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>

As you can notice at this part, exist a Converter called DateTimeFormatter at the time to make binding to the Text property of the "PART_TextBox". This converter receives the converterparameter that includes your custom format. Finally we add the code in C# for the DateTimeFormatter converter.

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

I hope this help to you. Please let me know for any issue or suggesting for improve.

丢了幸福的猪 2024-07-21 13:09:32
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";      
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "dd-MMM-yyyy";      
靑春怀旧 2024-07-21 13:09:32

将此样式添加到您的 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>

Add this style to your xaml or App.xaml file

 <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>
街角卖回忆 2024-07-21 13:09:32

在 XAML 中:

<toolkit:DatePicker SelectedDateFormat="Long" />

<toolkit:DatePicker SelectedDateFormat="Short" />

In XAML:

<toolkit:DatePicker SelectedDateFormat="Long" />

or

<toolkit:DatePicker SelectedDateFormat="Short" />
灰色世界里的红玫瑰 2024-07-21 13:09:32

感谢@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;
        }
    }
}

Thanks to @Fernando García for the basis of this.

I have written a DateFormat attached property for DatePicker that lets you provide a format string for display and input.

For input it will attempt to parse using the provided format, falling back to trying to parse it with the current culture's format.

Example usage with the question's format:

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

The DateFormat attached property is:

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;
        }
    }
}
物价感观 2024-07-21 13:09:32
DatePicker1.SelectedDate = DatePicker1.SelectedDate.Value.ToString("dd/MM/yyyy")
DatePicker1.SelectedDate = DatePicker1.SelectedDate.Value.ToString("dd/MM/yyyy")
虚拟世界 2024-07-21 13:09:32

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

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

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

dateTimePicker.SelectedDate.ToString("dd-MMM-yyyy");

Typically the date time format is stored in a resource file, because this would help in internationalization of the app.

You can pick up the format from the resource file and use a ToString(DATE_FORMAT)

In your case you might want to use

dateTimePicker.SelectedDate.ToString("dd-MMM-yyyy");
梦忆晨望 2024-07-21 13:09:32

对我来说,改变环境来改变 DatePicker 格式(如 Thread.CurrentCulture)并不是一个好主意。
当然,您可以创建从 DatePicker 派生的 Control 并实现像 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 的方式相同>。

As for me, changing environment to change DatePicker format (like Thread.CurrentCulture) is not a good idea.
Sure, you can create Control derived from DatePicker and implement dependency property like Format, but this costs too much effort.

Simple and elegant solution I found is binding value not to SelectedDate itself, but to some unused property (I used ToolTip property for this) and update this property when SelectedDate is changed.

C# implementation for one-way binding looks like this:

    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# should look like this:

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

For two-way implementation handle ToolTipChanged event the same way to update SelectedDate.

独享拥抱 2024-07-21 13:09:32

尝试这个

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
      DateLabel.Content = Convert.ToDateTime(datePicker1.Text).ToString("dd-MM-yyyy");
    }

Try This

    private void UserControl_Loaded(object sender, RoutedEventArgs e)
    {
      DateLabel.Content = Convert.ToDateTime(datePicker1.Text).ToString("dd-MM-yyyy");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文