我想DatePicker
将以下文本转换为DateTime
(以国际格式显示),以便我的客户可以更快地在文本框中写入日期,DatePicker
所以这不仅仅是DateTime.Parse
我会使用的:
"3" to 2009-10-03
"14" to 2009-10-14
"1403" to 2009-03-14
"140310" to 2010-03-14
"14032010" to 2010-03-14
我尝试了不同的方法来做到这一点,但它们不起作用。我试图用自定义值转换器绑定DatePicker.Text
// 。但它不起作用,因为在我到达文本之前已经处理了文本。DatePicker.SelectedDate
DatePicker.DisplayDate
DatePicker
我也尝试过这样的转换DatePicker
TextBox.LostFocus
:
public class CustomDatePicker : DatePicker
{
private DatePickerTextBox textBox;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
textBox = this.GetTemplateChild("TextBox") as DatePickerTextBox;
if (textBox != null)
textBox.LostFocus += textBox_LostFocus;
}
void textBox_LostFocus(object sender, RoutedEventArgs e)
{
if (textBox == null) return;
DateTime result;
// This is my method for converting my special cases,
// parses d, dd, mm, ddmm, ddmmyy, ddmmyyyy
if (textBox.Text.TryParseShortcut(out result))
{
// I have also tried with
// this.SelectedDate/DisplayDate = result;
textBox.Text = result.ToString("dd/MM/yyyy");
return;
}
if (DateTime.TryParse(textBox.Text, out result))
{
// I have also tried with
// this.SelectedDate/DisplayDate = result;
textBox.Text = result.ToString("dd/MM/yyyy");
return;
}
}
}
有任何想法吗?