我需要更改WPF Toolkit DatePicker中DatePickerTextBox的字符串格式,以便为分隔符使用连字符而不是斜杠。更改WPF DatePicker的字符串格式
有没有办法来覆盖这种默认文化或显示字符串格式?
01-01-2010
我需要更改WPF Toolkit DatePicker中DatePickerTextBox的字符串格式,以便为分隔符使用连字符而不是斜杠。更改WPF DatePicker的字符串格式
有没有办法来覆盖这种默认文化或显示字符串格式?
01-01-2010
我已经解决了这个问题,这个代码的帮助。希望它也能帮助你。
<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>
不幸的是,如果您在谈论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
}
它的出现,按照Wonko的回答,你不能指定Xaml格式的日期格式或从DatePicker继承。
我已经把下面的代码到我的视图的构造函数覆盖ShortDateFormat当前线程:
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
Thread.CurrentThread.CurrentCulture = ci;
的WPF工具包DateTimePicker
现在有一个Format
属性和FormatString
财产。如果您指定Custom
作为格式类型,则可以提供自己的格式字符串。
<wpftk:DateTimePicker
Value="{Binding Path=StartTime, Mode=TwoWay}"
Format="Custom"
FormatString="MM/dd/yyyy hh:mmtt"/>
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"/>
希望它可以帮助表现出依赖于位置
格式,但是这可以通过写这可避免:
!10而且你会很高兴(DD“ - ” MM“ - ” YYY)
接受的答案(感谢@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>
完美!像一个魅力工作:-) – MaYaN 2017-07-26 16:13:14
XAML
<DatePicker x:Name="datePicker" />
C#
var date = Convert.ToDateTime(datePicker.Text).ToString("yyyy/MM/dd");
把什么都格式化你想的ToString( “”)为例的ToString( “DD MMM YYY”)和输出格式为2017年6月7日
thx,它帮助我完全解决了使用日期选择器只选择月份。我在http://stackoverflow.com/questions/1798513/wpf-toolkit-datepicker-month-year-only/14902905#14902905中引用了你的xaml。 – GameAlchemist 2013-02-15 20:36:05
这种方法的巨大风扇。我更喜欢通过改变文化来做到这一点。 – Dom 2013-04-03 16:27:13
这是真正的答案!目前,肮脏的黑客被标记为答案。可怜。 – dzendras 2013-04-14 23:10:34