2

我想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.SelectedDateDatePicker.DisplayDateDatePicker

我也尝试过这样的转换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; 
        }
    }
}

有任何想法吗?

4

2 回答 2

1

如果您使用 Binding 将数据绑定到 DatePicker,则可以使用 ValueConverter。

日期转换器的快速示例:

public class CustomDateValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is string)
        {
            string dateValue = (string)value;
            switch (dateValue.Length)
            {
                case 1:
                case 2:
                    return new DateTime(DateTime.Now.Year, DateTime.Now.Month, System.Convert.ToInt32(dateValue)).ToString("dd/MM/yyyy");
                //...
            }
        }

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        // here you should implement your logic to convert your value back.

        return value;
    }
}

XAML 中的 DatePickerTextBox 控件:

<controls:DatePickerTextBox Text="{Binding Path=Data, Mode=TwoWay, Converter={StaticResource CustomDateValueConverter}}" />

cs 中的 DatePickerTextBox:

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();
        DataContext = new MyData() { Data = "3" };

        _textBox.LostFocus += (se, ea) =>
            {
                _textBox.DataContext = 
                    new MyData() { Data = _textBox.Text };
            };
}

public class MyData
{
    public string Data { get; set; }
}

我刚刚创建了一个名为 MyData 的类,用作此示例中的数据。

于 2009-10-21T18:32:40.563 回答
0

我认为您可能需要实际启动完整的控制源并对其进行自定义。问题是文本在 TextChanged 事件中得到更新:

private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (this._textBox != null)
    {
        this.SetValueNoCallback(TextProperty, this._textBox.Text);
    }
}

因此,当您找到丢失的焦点代码时,该值已经设置好了。我可以使用子类化来解决这个问题,但是由于 TextChanged 处理每个更改,我不确定您的 parse 方法是否有效(因为一旦您点击 1 个数字,它就会更改为日期)。

public class CustomDatePicker : DatePicker
{
    private TextBox textBox; 

    public override void OnApplyTemplate() 
    { 
        base.OnApplyTemplate(); 
        textBox = this.GetTemplateChild("TextBox") as TextBox; 

        if (textBox != null)
            textBox.TextChanged += textBox_TextChanged;
    }

    void textBox_TextChanged(object sender, TextChangedEventArgs e)
    {  
        if (textBox.Text == "now")             
        {                
            // I have also tried with 
            // this.SelectedDate/DisplayDate = result;                
            textBox.Text = DateTime.Now.ToShortDateString();                 
        }  
    }
}

它对我有用,因为在拼写整个单词之前没有任何变化,但在你的情况下,这可能行不通。

于 2009-10-21T22:24:07.690 回答