Event when the input is empty?

2 posts, 0 answers
  1. haagel
    haagel avatar
    52 posts
    Member since:
    Feb 2010

    Posted 19 Jan 2011 Link to this post

    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?
  2. Kaloyan
    Admin
    Kaloyan avatar
    920 posts

    Posted 21 Jan 2011 Link to this post

    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>>
  3. UI for WPF is Visual Studio 2017 Ready
Back to Top