This is a migrated thread and some comments may be shown as answers.

Event when the input is empty?

1 Answer 136 Views
DatePicker
This is a migrated thread and some comments may be shown as answers.
haagel
Top achievements
Rank 1
haagel asked on 19 Jan 2011, 03:42 PM
I'm extending the RadDatePicker. I have implemented a new property that tells whether the text input in the DatePicker is valid (that is if can be parsed to a DateTime):

/// <summary>
/// Tells whether the input in the TextBox of this DatePicker is valid. This property is
/// updated continously as the user types in the TextBox.
/// </summary>
public bool IsInputValid
{
    get
    {
        return (bool)GetValue(RadDatePickerExtended.IsInputValidProperty);
    }
    set
    {
        SetValue(RadDatePickerExtended.IsInputValidProperty, value);
    }
}
 
public static readonly DependencyProperty IsInputValidProperty = DependencyProperty.Register("IsInputValid", typeof(bool), typeof(RadDatePickerExtended), new FrameworkPropertyMetadata(true));


I'm setting the value of this property in an event handler for the ParseDateTimeValue event:

private void RadDatePickerExtended_ParseDateTimeValue(object sender, ParseDateTimeEventArgs args)
{
    //If RadDatePicker could not parse the input...
    if (!args.IsParsingSuccessful)
    {  
        //HERE I DO SOME EXTRA PARSING FOR INPUT THAT I WANT TO SUPPORT
        //BUT THAT IS NOT SUPPORTED BY RadDatePicker. IN ALL CASES
        //WHERE THE PARSING IS SUCCESFUL AND A DATETIME IS SET, I
        //ALSO SET args.IsParsingSuccessful TO TRUE.
 
        IsInputValid = args.IsParsingSuccessful;
    }
    else
    {
        //The input was succesfully parsed by the default parsing in the RadDatePicker.
        IsInputValid = true;
    }
}


This seems to work fine. But the problem is that if the user deletes the text in the input the event is NOT dispatched and thus my method is not executed. If the user provides some invalid input so that my IsInputValid property is set to false and then clears the input, IsInputValid will still be false! I want to set it to true when the input is empty (this will mean that the value of the DatePicker is null which is allowed in my case).

So my question is: Is it possible to get notified in some way when the input is cleared by the user?

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 21 Jan 2011, 01:44 PM
Hi David Haglund,

You can attach to the CurrentDateTimeTextProperty property change and check the current input for an empty value. Follow the code.

public class DatePickerExtended : RadDatePicker
{
    static DatePickerExtended()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(DatePickerExtended), new FrameworkPropertyMetadata(typeof(DatePickerExtended)));
        CurrentDateTimeTextProperty.AddOwner(typeof(DatePickerExtended), new FrameworkPropertyMetadata((s, e) =>
            {
                //// Here you can get the current input: e.NewValue
            }));
    }
    protected override void OnParseDateTime(ParseDateTimeEventArgs e)
    {
        base.OnParseDateTime(e);
    }
}

All the best,
Kaloyan
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
DatePicker
Asked by
haagel
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or