Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1415
Binding to invalid property to disable Button
posted

I have 2 - XamDateTimeInput controls (Start & End dates), and I want to disable a Button control whenever either of these XamDateTimeInput controls have invalid input.  Is there a way to do this in XAML?

Parents
  • 6365
    Verified Answer
    Offline posted

    Hello PMac,

    By saying that you would like to disable a Button control whenever any of the XamDateTimeInputs have an invalid input, I presume you mean that the value of the input is not a valid DateTime instance.
    If this is the case, since the XamDateTimeInput does not introduce a property that indicates whether the value is a valid DateTime object or not, an approach I can suggest you is to manually perform the validation operation in code-behind by handling the ValueChanged event of both Inputs with a shared handler.

    XAML:

    <ig:XamDateTimeInput x:Name="inputA" ValueChanged="input_ValueChanged" />
    <ig:XamDateTimeInput x:Name="inputB" ValueChanged="input_ValueChanged" /> 

    Code-behind:
        private void input_ValueChanged(object sender, EventArgs e)
    {
                if (IsValidDateTime(inputA.Text) && IsValidDateTime(inputB.Text))
                    button.IsEnabled = true;
                else
                    button.IsEnabled = false;
    }

    private bool IsValidDateTime(string text)
    {
                DateTime temp;
                return DateTime.TryParse(text, out temp);
    }

    This way whenever the value of one of the controls has been changed, the ValueChanged event will fire and the Button will get enabled or disabled respectively based on whether both controls have a valid DateTime value or not.

    I have attached a sample application, where the approach from above has been used.

    If you require any further assistance on this matter, please do not hesitate to ask.
     

    XamDateTimeInput_validation.zip
Reply Children