WPF文本框的ValidationRule

我是WPF的新手。在我的UserControl中,我有8个标签及其各自的8个文本框,如下所示:

1.Label : abc 2.Label : def TextBox1 : TextBox2 : 3.Label :xyz 4. Label : ghi Textbox3 : TextBox4 : 

这些文本框文本属性中的每一个都应该包含以TextBox1.text各自的标签名称结尾的文本,应该是xxxx.abcTextBox2.text应该是xxxx.def等等。如果文本框不应该有红色边框。

希望我对细节很清楚。所以我需要为每个文本框编写不同的ValidationRule吗?

你输入的是什么?

为什么没有一个ValidationRule实现,其中一个属性暴露了该字段应该以什么结束,例如:

 public class EndsWithValidationRule : ValidationRule { public string MustEndWith { get; set; } public override ValidationResult Validate(object value, CultureInfo cultureInfo) { var str = value as string; if(str == null) { return new ValidationResult(false, "Please enter some text"); } if(!str.EndsWith(MustEndWith)) { return new ValidationResult(false, String.Format("Text must end with '{0}'", MustEndWith)); } return new ValidationResult(true, null); } } 

然后您可以这样使用: