【问题标题】:Changing the string format of the WPF DatePicker更改 WPF DatePicker 的字符串格式
【发布时间】:2011-04-18 16:52:31
【问题描述】:

我需要更改 WPF Toolkit DatePicker 中 DatePickerTextBox 的字符串格式,以使用连字符而不是斜杠作为分隔符。

有没有办法覆盖这种默认文化或显示字符串格式?

01-01-2010

【问题讨论】:

    标签: wpf datepicker wpftoolkit


    【解决方案1】:

    我在这段代码的帮助下解决了这个问题。希望对大家也有帮助。

    <Style TargetType="{x:Type DatePickerTextBox}">
     <Setter Property="Control.Template">
      <Setter.Value>
       <ControlTemplate>
        <TextBox x:Name="PART_TextBox"
         Text="{Binding Path=SelectedDate, StringFormat='dd MMM yyyy', 
         RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
       </ControlTemplate>
      </Setter.Value>
     </Setter>
    </Style>
    

    【讨论】:

    • 谢谢,它帮助我完全解决了使用日期选择器只选择月份的问题。我在stackoverflow.com/questions/1798513/… 中引用了您的 xaml。
    • 这种方法的超级粉丝。我更喜欢这样做而不是改变文化。
    • 这才是真正的答案!目前,肮脏的黑客被标记为答案。可惜。
    • 我可以看到这种方法存在一些问题,例如: 1. 没有“选择日期”水印; 2. 用户输入的日期(Text)不会反映回SelectedDate。 @benPearce,你有更好的解决方案吗?
    • 当我尝试手动/文本编辑日期时,我遇到了这个解决方案的问题。我使用的格式是:dd/MM/yyyy。如果我输入“11/01/1”,在失去焦点时,它会自动变为 11/01/2001... 如果我输入“11/01/201”,在失去焦点时,它会自动变为 11/ 01/0201...所有这一切都说明此自动完成的行为是不可预测的,我想禁用此功能...如果一年中没有 4 位数字,我宁愿出错...跨度>
    【解决方案2】:

    根据 Wonko 的回答,您似乎无法以 Xaml 格式或从 DatePicker 继承来指定日期格式。

    我已将以下代码放入 View 的构造函数中,该构造函数会覆盖当前线程的 ShortDateFormat:

    CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
    ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
    Thread.CurrentThread.CurrentCulture = ci;
    

    【讨论】:

    • 这实际上是我更喜欢的,因为这不仅使整个系统的日期格式保持一致,而且我还注意到与 datepicker 样式混淆会弄乱诸如占位符文本之类的东西。
    • 最好最简单的方法!如果将此代码放在App.cs 构造函数中,则可以跨应用程序工作:)
    【解决方案3】:

    WPF 工具包DateTimePicker 现在有一个Format 属性和一个FormatString 属性。如果您指定Custom作为格式类型,您可以提供自己的格式字符串。

    <wpftk:DateTimePicker
        Value="{Binding Path=StartTime, Mode=TwoWay}"
        Format="Custom"
        FormatString="MM/dd/yyyy hh:mmtt"/>
    

    【讨论】:

    • DateTimePicker 与 DatePicker 不同,所以这实际上并不能回答问题
    【解决方案4】:

    接受的答案(感谢@petrycol)让我走上了正确的道路,但我在实际的日期选择器中获得了另一个文本框边框和背景颜色。使用以下代码修复它。

            <Style TargetType="{x:Type Control}" x:Key="DatePickerTextBoxStyle">
                <Setter Property="BorderThickness" Value="0"/>
                <Setter Property="VerticalAlignment" Value="Center"/>
                <Setter Property="Background" Value="{x:Null}"/>
            </Style>
    
            <Style TargetType="{x:Type DatePickerTextBox}" >
                <Setter Property="Control.Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <TextBox x:Name="PART_TextBox"
                                 Text="{Binding Path=SelectedDate, StringFormat='dd-MMM-yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" Style="{StaticResource DatePickerTextBoxStyle}" >
                            </TextBox>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    

    【讨论】:

    • 此解决方案有任何变化
    【解决方案5】:

    注意:此答案(最初写于 2010 年)适用于早期版本。查看其他关于在较新版本中使用自定义格式的答案

    不幸的是,如果您在谈论 XAML,您将无法将 SelectedDateFormat 设置为“Long”或“Short”。

    如果您下载了工具包的源代码以及二进制文件,您可以看到它是如何定义的。以下是该代码的一些亮点:

    DatePicker.cs

    #region SelectedDateFormat
    
    /// <summary>
    /// Gets or sets the format that is used to display the selected date.
    /// </summary>
    public DatePickerFormat SelectedDateFormat
    {
        get { return (DatePickerFormat)GetValue(SelectedDateFormatProperty); }
        set { SetValue(SelectedDateFormatProperty, value); }
    }
    
    /// <summary>
    /// Identifies the SelectedDateFormat dependency property.
    /// </summary>
    public static readonly DependencyProperty SelectedDateFormatProperty =
        DependencyProperty.Register(
        "SelectedDateFormat",
        typeof(DatePickerFormat),
        typeof(DatePicker),
        new FrameworkPropertyMetadata(OnSelectedDateFormatChanged),
        IsValidSelectedDateFormat);
    
    /// <summary>
    /// SelectedDateFormatProperty property changed handler.
    /// </summary>
    /// <param name="d">DatePicker that changed its SelectedDateFormat.</param>
    /// <param name="e">DependencyPropertyChangedEventArgs.</param>
    private static void OnSelectedDateFormatChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        DatePicker dp = d as DatePicker;
        Debug.Assert(dp != null);
    
        if (dp._textBox != null)
        {
            // Update DatePickerTextBox.Text
            if (string.IsNullOrEmpty(dp._textBox.Text))
            {
                dp.SetWaterMarkText();
            }
            else
            {
                DateTime? date = dp.ParseText(dp._textBox.Text);
    
                if (date != null)
                {
                    dp.SetTextInternal(dp.DateTimeToString((DateTime)date));
                }
            }
        }
    }
    
    
    
    #endregion SelectedDateFormat
    
    private static bool IsValidSelectedDateFormat(object value)
    {
        DatePickerFormat format = (DatePickerFormat)value;
    
        return format == DatePickerFormat.Long
            || format == DatePickerFormat.Short;
    }
    
    private string DateTimeToString(DateTime d)
    {
        DateTimeFormatInfo dtfi = DateTimeHelper.GetCurrentDateFormat();
    
        switch (this.SelectedDateFormat)
        {
            case DatePickerFormat.Short:
                {
                    return string.Format(CultureInfo.CurrentCulture, d.ToString(dtfi.ShortDatePattern, dtfi));
                }
    
            case DatePickerFormat.Long:
                {
                    return string.Format(CultureInfo.CurrentCulture, d.ToString(dtfi.LongDatePattern, dtfi));
                }
        }      
    
        return null;
    }
    

    DatePickerFormat.cs

    public enum DatePickerFormat
    {
        /// <summary>
        /// Specifies that the date should be displayed 
        /// using unabbreviated days of the week and month names.
        /// </summary>
        Long = 0,
    
        /// <summary>
        /// Specifies that the date should be displayed 
        ///using abbreviated days of the week and month names.
        /// </summary>
        Short = 1
    }
    

    【讨论】:

      【解决方案6】:

      XAML

       <DatePicker x:Name="datePicker" />
      

      C#

      var date = Convert.ToDateTime(datePicker.Text).ToString("yyyy/MM/dd");
      

      在 ToString("") 中输入您想要的任何格式,例如 ToString("dd MMM yyy"),输出格式将为 2017 年 6 月 7 日

      【讨论】:

      • 这里有一个异常,请查看您的回答:mscorlib.dll 字符串中发生的“System.FormatException”类型的未处理异常未被识别为有效的日期时间。
      【解决方案7】:

      转换器类:

      public class DateFormat : IValueConverter
      {
          public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              if (value == null) return null;
              return ((DateTime)value).ToString("dd-MMM-yyyy");
          }
      
          public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
          {
              throw new NotImplementedException();
          }
      }
      

      wpf 标签

      <DatePicker Grid.Column="3" SelectedDate="{Binding DateProperty, Converter={StaticResource DateFormat}}" Margin="5"/>
      

      希望对你有帮助

      【讨论】:

        【解决方案8】:

        显示的格式取决于位置,但可以通过编写以下代码来避免这种情况:

          ValueStringFormat="{}{0:MM'-'yy}" />
        

        你会很开心!(dd'-'MM'-'yyy)

        【讨论】:

          【解决方案9】:

          正如 Ben Pearce 回答的那样,我们可以使用 CultureInfo 类来处理自定义格式, 我真的认为是唯一合乎逻辑的方法。如果您有不同格式的 dateTimePickers,您可以使用:

          CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
          ci.DateTimeFormat.LongDatePattern = "MMM.yyyy"; //This can be used for one type of DatePicker
          ci.DateTimeFormat.ShortDatePattern = "dd.MMM.yyyy"; //for the second type
          Thread.CurrentThread.CurrentCulture = ci;
          

          然后您可以更改 .xaml 文档中的 DateFormat。

            <DatePicker Name="dateTimePicker1" SelectedDateFormat="Long"  />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2017-07-05
            • 2013-05-02
            • 1970-01-01
            • 2011-05-04
            • 1970-01-01
            • 2016-06-25
            • 1970-01-01
            相关资源
            最近更新 更多