
基本上,我有一个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
以上是内存溢出为你收集整理的WPF – 绑定ValidationRules到Combobox.Text属性全部内容,希望文章能够帮你解决WPF – 绑定ValidationRules到Combobox.Text属性所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)