2010-09-29 126 views
31

我需要更改WPF Toolkit DatePicker中DatePickerTextBox的字符串格式,以便爲分隔符使用連字符而不是斜槓。更改WPF DatePicker的字符串格式

有沒有辦法來覆蓋這種默認文化或顯示字符串格式?

01-01-2010 

回答

76

我已經解決了這個問題,這個代碼的幫助。希望它也能幫助你。

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

thx,它幫助我完全解決了使用日期選擇器只選擇月份。我在http://stackoverflow.com/questions/1798513/wpf-toolkit-datepicker-month-year-only/14902905#14902905中引用了你的xaml。 – GameAlchemist 2013-02-15 20:36:05

+2

這種方法的巨大風扇。我更喜歡通過改變文化來做到這一點。 – Dom 2013-04-03 16:27:13

+0

這是真正的答案!目前,骯髒的黑客被標記爲答案。可憐。 – dzendras 2013-04-14 23:10:34

5

不幸的是,如果您在談論XAML,那麼您將SelectedDateFormat設置爲「長」或「短」是堅持不懈的。

如果您下載了工具包的源代碼以及二進制文件,您可以看到它是如何定義的。下面是一些代碼的亮點:

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

它的出現,按照Wonko的回答,你不能指定Xaml格式的日期格式或從DatePicker繼承。

我已經把下面的代碼到我的視圖的構造函數覆蓋ShortDateFormat當前線程:

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

的WPF工具包DateTimePicker現在有一個Format屬性和FormatString財產。如果您指定Custom作爲格式類型,則可以提供自己的格式字符串。

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

Converter類:

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

希望它可以幫助表現出依賴於位置

0

格式,但是這可以通過寫這可避免:

!10

而且你會很高興(DD「 - 」 MM「 - 」 YYY)

9

接受的答案(感謝@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> 
+0

完美!像一個魅力工作:-) – MaYaN 2017-07-26 16:13:14

1

XAML

<DatePicker x:Name="datePicker" /> 

C#

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

把什麼都格式化你想的ToString( 「」)爲例的ToString( 「DD MMM YYY」)和輸出格式爲2017年6月7日