我正在尝试做一些我以前认为很容易的事情:在另一个控件的验证规则中使用来自一个控件的值。我的应用程序有多种用户可以输入的参数,这里讨论的具体参数定义了范围的起点和终点,用户通过文本框设置值。
有问题的两个控件是开始和结束文本框,在验证中应检查以下条件:
- 起始值必须大于或等于某个任意值
- 结束值必须小于或等于某个任意值
- 起始值必须小于或等于结束值
前两个条件我已经完成了。第三个实现起来要困难得多,因为我无法从验证器访问结束文本框的值。即使可以,我也尝试验证五个不同的范围(每个范围都有自己的开始和结束文本框),并且必须有一些比为每个范围创建验证规则更优雅的解决方案。
这是相关的 XAML 代码:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:validators="clr-namespace:CustomValidators"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Name="textboxStart" Grid.Row="0">
<TextBox.Text>
<Binding Path="Start" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:MeasurementRangeRule Min="1513" Max="1583"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
<TextBox Name="textboxEnd" Grid.Row="1">
<TextBox.Text>
<Binding Path="End" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:MeasurementRangeRule Min="1513" Max="1583"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
这是相关的 C# 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.CompilerServices;
using System.ComponentModel;
using System.Globalization;
namespace WpfApplication1 {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow () {
InitializeComponent();
}
private decimal _start;
private decimal _end;
public event PropertyChangedEventHandler PropertyChanged;
public decimal Start {
get { return _start; }
set {
_start = value;
RaisePropertyChanged();
}
}
public decimal End {
get { return _end; }
set {
_end = value;
RaisePropertyChanged();
}
}
private void RaisePropertyChanged ([CallerMemberName] string propertyName = "") {
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
namespace CustomValidators {
public class MeasurementRangeRule : ValidationRule {
private decimal _min;
private decimal _max;
public decimal Min {
get { return _min; }
set { _min = value; }
}
public decimal Max {
get { return _max; }
set { _max = value; }
}
public override ValidationResult Validate (object value, CultureInfo cultureInfo) {
decimal measurementParameter = 0;
try {
if (((string) value).Length > 0)
measurementParameter = Decimal.Parse((String) value);
} catch (Exception e) {
return new ValidationResult(false, "Illegal characters or " + e.Message);
}
if ((measurementParameter < Min) || (measurementParameter > Max)) {
return new ValidationResult(false,
"Out of range. Enter a parameter in the range: " + Min + " - " + Max + ".");
} else {
return new ValidationResult(true, null);
}
}
}
}
这里链接的问题似乎是相关的,但我无法理解所提供的答案。
谢谢...