WPF – 绑定ValidationRules到Combobox.Text属性

WPF – 绑定ValidationRules到Combobox.Text属性,第1张

概述我正在使用其IsEditable值设置为true的 WPF ComboBox. 基本上,我有一个ComboBox中显示的项目列表.如果用户在ComboBox中找不到合适的时间,则可以输入时间. 我已将ValidationRule附加到我的ComboBox.SelectedItem,以便每当用户选择一个时间时,我的ValidationClass被调用(从ValidationRule派生).这一切都很 我正在使用其IsEditable值设置为true的 WPF ComboBox.
基本上,我有一个ComboBox中显示的项目列表.如果用户在ComboBox中找不到合适的时间,则可以输入时间.

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

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

谁能告诉我如何验证用户输入的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

@H_403_32@解决方法 我知道现在为时已晚,但也许这会对某人有所帮助: 绑定到ComboBox的Text属性,而不是SelectedItem 总结

以上是内存溢出为你收集整理的WPF – 绑定ValidationRules到Combobox.Text属性全部内容,希望文章能够帮你解决WPF – 绑定ValidationRules到Combobox.Text属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1039958.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-24
下一篇2022-05-24

发表评论

登录后才能评论

评论列表(0条)

    保存