【发布时间】:2011-04-18 16:52:31
【问题描述】:
我需要更改 WPF Toolkit DatePicker 中 DatePickerTextBox 的字符串格式,以使用连字符而不是斜杠作为分隔符。
有没有办法覆盖这种默认文化或显示字符串格式?
01-01-2010
【问题讨论】:
标签: wpf datepicker wpftoolkit
我需要更改 WPF Toolkit DatePicker 中 DatePickerTextBox 的字符串格式,以使用连字符而不是斜杠作为分隔符。
有没有办法覆盖这种默认文化或显示字符串格式?
01-01-2010
【问题讨论】:
标签: wpf datepicker wpftoolkit
我在这段代码的帮助下解决了这个问题。希望对大家也有帮助。
<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>
【讨论】:
Text)不会反映回SelectedDate。 @benPearce,你有更好的解决方案吗?
根据 Wonko 的回答,您似乎无法以 Xaml 格式或从 DatePicker 继承来指定日期格式。
我已将以下代码放入 View 的构造函数中,该构造函数会覆盖当前线程的 ShortDateFormat:
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
Thread.CurrentThread.CurrentCulture = ci;
【讨论】:
App.cs 构造函数中,则可以跨应用程序工作:)
WPF 工具包DateTimePicker 现在有一个Format 属性和一个FormatString 属性。如果您指定Custom作为格式类型,您可以提供自己的格式字符串。
<wpftk:DateTimePicker
Value="{Binding Path=StartTime, Mode=TwoWay}"
Format="Custom"
FormatString="MM/dd/yyyy hh:mmtt"/>
【讨论】:
接受的答案(感谢@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>
【讨论】:
注意:此答案(最初写于 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
}
【讨论】:
XAML
<DatePicker x:Name="datePicker" />
C#
var date = Convert.ToDateTime(datePicker.Text).ToString("yyyy/MM/dd");
在 ToString("") 中输入您想要的任何格式,例如 ToString("dd MMM yyy"),输出格式将为 2017 年 6 月 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"/>
希望对你有帮助
【讨论】:
显示的格式取决于位置,但可以通过编写以下代码来避免这种情况:
ValueStringFormat="{}{0:MM'-'yy}" />
你会很开心!(dd'-'MM'-'yyy)
【讨论】:
正如 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" />
【讨论】: