Hooking Silverlight DatePicker convert from text to DateTime

I want to DatePicker

convert the following text snippets to DateTime

(shown in international format) so that my clients can write the date in the textbox faster DatePicker

, so I won't use 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

      

I've tried different methods but they don't work. I tried to bind DatePicker.Text

/ DatePicker.SelectedDate

/ DatePicker.DisplayDate

to a custom evaluated converter. But it doesn't work because it DatePicker

has already processed the text before I get the text.

I also tried converting to DatePicker

TextBox.LostFocus

like this:

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; 
        }
    }
}

      

Any ideas?

+2


source to share


2 answers


If you are using Binding to bind data to your DatePicker, you can use ValueConverter.

A quick example of a date converter:

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;
    }
}

      

Controlling DatePickerTextBox in XAML:



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

      

DatePickerTextBox in cs:

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; }
}

      

I just created the MyData class to use as data in this example.

+1


source


I think you might need to actually run the full control source and configure it. The problem is that the text is updated in the TextChanged event:

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

      

So, by the time you get to your lost focus code, the value is already set. I managed to get around this by subclassing, but since TextChanged handles every change, I'm not sure if your parse method will work (since as soon as you hit 1 number it will be changed to date).



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();                 
        }  
    }
}

      

It works for me since nothing changes until it has spoken all the words, but in your case it probably won't work.

0


source







All Articles