tags:

views:

568

answers:

1

Hi,

I'm using a WPF combobox whose IsEditable value is set to true.

Basically, I have a list of times displayed int he comobo box. User can himself type in a time if he does not find a suitable time in the combobox.

I have attached a ValidationRule to my ComboBox.SelectedItem so that whenever the user selects a time my ValidationClass is invoked (Derived from ValidationRule). All this works fine.

Since my combobox is Editable users can enter their own time. The validation class gets invoked everytime I type in a value int the combobox and the value that gets passed to that class is the value I type in. Now the problem is, if the user types in a value that is not a part of the comobbox item the validation class gets invoked with a null value and thus im not able to validate anything.

Can anyone tell me how to validate the ComboBox.Text item entered by the user?

My Validation class

public class TimeValidateRule : ValidationRule {

    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        TimeClass timeObj = value as TimeClass;
        TimeSpan time;

       if(timeObj == null)
            return new ValidationResult(false, "Invalid Time");

        if(timeObj.Time.Length < 5)
            return new ValidationResult(false, "Invalid Time");
        try
        {
            time = TimeSpan.Parse(timeObj.Time);
        }
        catch
        {
            return new ValidationResult(false, "Invalid Time");
        }

        // Get Current time (Arizona time)
        if(!CheckAgainstArizonaTime(time))
            return new ValidationResult(false, "Invalid Time");

        return new ValidationResult(true, null);
    }

Combobox declaration in xaml

                     <ComboBox ItemsSource="{Binding Source={StaticResource TimeSelections}}"
                          ItemTemplate="{StaticResource TimeListTemplate}"                              
                          Validation.ErrorTemplate="{StaticResource ValidationTemplate}"                              
                          Height="30" Width="100"                                  
                          Name="cbTimes"                              
                          >                                     
                    <ComboBox.SelectedItem>
                        <Binding 
                            Path="SelectedTime"                                
                            UpdateSourceTrigger="PropertyChanged"                               
                            >
                            <Binding.ValidationRules>
                                <validators:TimeValidateRule/>                                                       
                            </Binding.ValidationRules>
                        </Binding>
                    </ComboBox.SelectedItem>

                </ComboBox>

Thanks, Jithu

A: 

you need to handle the code and exceptions using event handler etc check on the net for further samples

abmv
There should be a better solution. Doing it in event handler is a long way around the "bug".
Maiku Mori