ValidationRulesUsingValuesfromAnotherControlvalue。我的应用程序有各种用户可以输入的参数,这里讨论的特定参数定义范围的开始和结束,用户通过文本框设置值。有问题的两个控件是开始和结束文本框,验证时应检查以下条件:起始值必须大于或等于某个任意值结束值必须小于或等于某个任意值起始值必须小于或等于我对前两个条件所做的最终值。第三个更难实现,因为我无法从验证器访问结束文本框的值。即使我可以,也有五个不同的范围(每个都有自己的开始和结束文本框)我正在尝试验证,并且必须有一些比为每个范围创建验证规则更优雅的解决方案。这是相关的XAML代码:这是相关的C#代码:usingSystem;使用System.Collections.Generic;使用System.Linq;使用系统文本;使用System.Threading.Tasks;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Data;使用System.Windows.Documents;使用System.Windows.Input;使用System.Windows.Media;使用System.Windows.Media.Imaging;使用System.Windows.Navigation;使用System.Windows.Shapes;使用System.Runtime.CompilerServices;使用System.ComponentModel;使用System.Globalization;namespaceWpfApplication1{//////MainWindow.xaml的交互逻辑///publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();}私人十进制_start;私人十进制_end;公共事件PropertyChangedEventHandlerPropertyChanged;publicdecimalStart{get{return_start;}设置{_start=值;RaisePropertyChanged();}}publicdecimalEnd{get{return_end;}设置{_end=val等;RaisePropertyChanged();}}privatevoidRaisePropertyChanged([CallerMemberName]stringpropertyName=""){if(PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs(propertyName));}}}}namespaceCustomValidators{publicclassMeasurementRangeRule:ValidationRule{privatedecimal_min;私有小数_max;publicdecimalMin{get{return_min;}设置{_min=值;}}publicdecimalMax{get{return_max;}设置{_max=值;}}publicoverrideValidationResultValidate(objectvalue,CultureInfocultureInfo){decimalmeasurementParameter=0;try{if(((string)value).Length>0)measurementParameter=Decimal.Parse((String)value);}catch(Exceptione){returnnewValidationResult(false,"非法字符或"+e.Message);}if((measurementParameterMax)){returnnewValidationResult(false,"Outofrange.Enteraparameterintherange:"+Min+"-"+Max+".");}else{返回新的V验证结果(真,空);此处链接的问题似乎是相关的,但我无法理解所提供的答案谢谢...对于可能遇到此问题的任何人,请实施IDataErrorInfo以更正常地验证错误,并且更容易验证其他一些逻辑分组中的控件。我将相关的属性(start、end、min、max)封装在一个类中,将控件绑定到这些属性上,然后使用IDataErrorInfo接口进行校验。相关代码如下...XAML:C#:以上是C#学习教程的全部内容:验证规则使用了另一个控件的值。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注usingSystem.Collections.Generic;使用System.Linq;使用系统文本;使用System.Threading.Tasks;使用System.Windows;使用System.Windows.Controls;使用System.Windows.Data;使用System.Windows.Input;使用System.Windows.Media;使用System.Windows.Media.Imaging;使用System.Windows.Navigation;使用System.Windows.Shapes;使用System.Runtime.CompilerServices;.组件模型;namespaceWpfApplication1{//////MainWindow.xaml的交互逻辑///publicpartialclassMainWindow:Window{publicMainWindow(){InitializeComponent();参数测试参数=新参数(0,10);测试网格。DataContext=测试参数;}}publicclassParameter:INotifyPropertyChanged,IDataErrorInfo{privatedecimal_start,_end,_min,_max;公共事件适当tyChangedEventHandler属性已更改;publicParameter(){}publicParameter(decimalmin,decimalmax){this.Min=min;这个.Max=max;}publicdecimalStart{get{return_start;}设置{_start=值;//Start和End的RaisePropertyChanged,因为一个可能因为另一个的当前设置而需要标记为无效。//例如Start>End,在这种情况下,根据既定条件,两个属性现在都无效,但只有最近更改的属性才会被验证RaisePropertyChanged();RaisePropertyChanged("结束");}}publicdecimalEnd{get{return_end;}设置{_end=值;//Start和End的RaisePropertyChanged,因为一个可能因为另一个的当前设置而需要标记为无效。//例如Start>End,在这种情况下,根据既定条件,两个属性现在都无效,但只有最近更改的属性才会被验证RaisePropertyChanged();RaisePropertyChanged("开始");}}publicdecimalMin{get{return_min;}设置{_min=值;}}publicdecimalMax{get{return_max;}设置{_max=值;}}privatevoidRaisePropertyChanged([CallerMemberName]stringpropertyName=""){if(PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs(propertyName));}}publicstringError{get{returnstring.Empty;}}publicstringthis[stringcolumnName]{get{stringresult=string.Empty;switch(columnName){case"Start":if(StartMax||Start>End){result="Outofrange.Enteravalueintherange:"+Min+"-"+End+".";}休息;case"End":if(EndMax||End:
