WPF - 绑定ValidationRules到Combobox.Text属性

时间:2009-09-09 08:48:36

标签: wpf validation combobox

我正在使用其ComboBox值设置为true的WPF IsEditable。 基本上,我有ComboBox中显示的项目列表。如果用户在ComboBox中找不到合适的时间,则可以输入时间。

我已将ValidationRule附加到ComboBox.SelectedItem,以便每当用户选择一个时间时,我的ValidationClass被调用(从ValidationRule派生)。这一切都很好。

由于我的ComboBox可编辑,因此用户可以输入自己的时间。每当我输入ComboBox的值时,都会调用验证类,并且传递给该类的值是我输入的值。现在的问题是,如果用户输入的值不是使用null值调用验证类的comobbox项的一部分,因此我无法验证任何内容。

有人能告诉我如何验证用户输入的ComboBox.Text项目吗?

我的验证课程:

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);
    }
}        
xaml中的

ComboBox声明:

                     <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>

谢谢, Jithu

3 个答案:

答案 0 :(得分:6)

我知道现在为时已晚,但也许这会对某人有所帮助: 绑定到Text的{​​{1}}属性,而不是ComboBox

答案 1 :(得分:1)

<ComboBox Name="myComboBox">                                     
    <ComboBox.Text>
        <Binding Path="SelectedTime" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <validators:TimeValidateRule/>                                                       
            </Binding.ValidationRules>
        </Binding>
    </ComboBox.Text>
</ComboBox>

答案 2 :(得分:0)

您需要使用事件处理程序等处理代码和异常,并在网上检查更多样本