Click here to Skip to main content
15,558,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TimeSpan textbox, my objectif is to disable the button Save when the form of TIMESPAN is wrong (exp 90:00:00)..

I try a code, it is correct for just once time..if I set 20:10:00 ..The Save button is enabled (correct). After that however the TIMESPAN is wrong 55:00:00, the button is enabled ( and the saving in database is 00:00:00)



How I can fix it to be work always correct? Thanks,


What I have tried:

The XAML:
<TextBox Name="txtTime"  Margin="10,10,10,10" >
                <TextBox.Text >
                    <Binding Path="Time" UpdateSourceTrigger="PropertyChanged" NotifyOnValidationError="True" Mode="TwoWay" >
                        <Binding.ValidationRules>
                            <local:DateTimeValidationRule ValidationStep="RawProposedValue"/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>


The ViewModel :


public bool  VarTIME ;



  [Required(ErrorMessage = "Time is required")]
    public TimeSpan Time
    {
        get { return time; }
        set
        {
            time = value;
            intervalString = Time.ToString();
            TimeSpan reded;
            bool success = TimeSpan.TryParseExact(intervalString, "hh\\:mm\\:ss",
                           CultureInfo.InvariantCulture, out reded);

            if (success)
            {
                VarTIME = true;
            }
            OnPropertyChanged("Time");
        }
    }


    public SheduleTrainViewModel()
    {
        VarTIME = false;
        addTrain = new RelayCommand<string>(AddTrainFunction, canAddTrain);

        private bool canAddTrain(string obj)
         {     return VarTIME;     

         }
    }
Posted
Updated 29-Nov-17 22:25pm
v2

1 solution

In this case it looks like it's failing because you're not setting VarTIME back to false on a failure. This should fix it:

public TimeSpan Time
{
    get { return time; }
    set
    {
        time = value;
        intervalString = Time.ToString();
        TimeSpan reded;

        VarTIME = TimeSpan.TryParseExact(intervalString, "hh\\:mm\\:ss",
                  CultureInfo.InvariantCulture, out reded);

        OnPropertyChanged("Time");
    }
}


That being said, I think there are a lot better approaches... The setter method here a call back to the property, it's parsing data that doesn't get set back to the backer, it's setting other variables...

Also I think you may have missed that TimeSpan is a struct so it will always have a value, therefore required isn't going to work here. Additionally for input purposes the default value of 0.00:00:00 may be valid.

If you want to make sure that the time is less than a day, use a required maximum attribute. When converting this to the DB model you can just truncate the fractional seconds if you don't want them.

For just making sure it's valid, a simple approach I would use is a TimeSpan? converter and return null if the format isn't as expected. Then change your command to take a TimeSpan? and return false if null.

For a more elegant solution I'd look at some of the examples for validation here:

WPF TextBox Validation C# - Stack Overflow[^]
 
Share this answer
 
Comments
ThabetMicrosoft 4-Dec-17 5:50am    
Thanks a lot ,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900