ben*_*rce 43 wpf datepicker wpftoolkit
我需要在WPF Toolkit DatePicker中更改DatePickerTextBox的字符串格式,以便为分隔符使用连字符而不是斜杠.
有没有办法覆盖此默认文化或显示字符串格式?
01-01-2010
Run Code Online (Sandbox Code Playgroud)
小智 95
我已经借助此代码解决了这个问题.希望它也能帮到你们所有人.
<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>
Run Code Online (Sandbox Code Playgroud)
ben*_*rce 22
根据Wonko的回答,您似乎无法以Xaml格式指定日期格式或继承DatePicker.
我已将以下代码放入我的View的构造函数中,该构造函数覆盖当前线程的ShortDateFormat:
CultureInfo ci = CultureInfo.CreateSpecificCulture(CultureInfo.CurrentCulture.Name);
ci.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
Thread.CurrentThread.CurrentCulture = ci;
Run Code Online (Sandbox Code Playgroud)
Jor*_*mer 14
WPF工具包DateTimePicker现在有一个Format属性和FormatString属性.如果指定Custom格式类型,则可以提供自己的格式字符串.
<wpftk:DateTimePicker
Value="{Binding Path=StartTime, Mode=TwoWay}"
Format="Custom"
FormatString="MM/dd/yyyy hh:mmtt"/>
Run Code Online (Sandbox Code Playgroud)
Mar*_*rkB 12
接受的答案(感谢@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>
Run Code Online (Sandbox Code Playgroud)
注意:此答案(最初写于2010年)适用于早期版本.有关使用较新版本的自定义格式,请参阅其他答案
不幸的是,如果你在谈论XAML,那么你很难将SelectedDateFormat设置为"Long"或"Short".
如果您下载了Toolkit的源代码以及二进制文件,则可以看到它是如何定义的.以下是该代码的一些亮点:
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;
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
77129 次 |
| 最近记录: |