我的错,实际上你不能使用 x:TypeArguments 因为它会引发x:TypeArguments is not allowed in object elements for XAML version below 2009,它仅对松散的 XAML 文件或根元素有效(在我的情况下为 Window )...
http://msdn.microsoft.com/en-us/library/ms750476.aspx
但作为一种解决方法,您可以使用以下模式:
<TextBox x:Name="textBox1">
<Binding Path="MyValue"
Source="{StaticResource MyObject}"
UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<wpfApplication6:MyValidationRule ObjectType="{x:Type system:Int32}" />
</Binding.ValidationRules>
</Binding>
</TextBox>
后面的代码:
public class MyObject
{
public object MyValue { get; set; }
}
public class MyValidationRule : ValidationRule
{
public Type ObjectType { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
throw new NotImplementedException();
}
}
也看看这个:
Using a Generic IValueConverter from XAML
@Blam 评论值得考虑,要检查的范围通常适用于整数或双精度数,对于其他类型,我想说您可以添加一个返回该对象有效性的布尔值并在对象本身内部执行此类验证。
对于数字,您有RangeAttribute,但它并不是 WPF 验证基础结构的真正组成部分。
您还有另一个验证选项:在这种情况下, INotifyDataErrorInfo验证发生在对象内部。
我在这里写了一个冗长的答案:https ://softwareengineering.stackexchange.com/questions/203590/is-there-an-effective-way-for-creating-complex-forms你可能会发现一些有用的东西。
根据我的经验,我会说通用的验证规则可能并不明智。
您应该将您的问题编辑为不那么通用;-) 但更具体,您会从这里的人那里获得更多帮助。给出你要验证的对象的一两个具体案例。
编辑
您还可以使用 BindingGroup 来验证对象:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TextBox1_OnTextChanged(object sender, TextChangedEventArgs e)
{
grid.BindingGroup.CommitEdit();
}
}
public class Indices
{
public int ColorIndex { get; set; }
public string ColorPrefix { get; set; }
public int GradientIndex { get; set; }
public string GradientPrefix { get; set; }
}
public class ValidateMe : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var bindingGroup = value as BindingGroup;
var o = bindingGroup.Items[0] as Indices;
return new ValidationResult(true, null);
}
}
<Window x:Class="WpfApplication6.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication6="clr-namespace:WpfApplication6"
Title="MainWindow"
Width="525"
Height="350">
<Window.Resources>
<wpfApplication6:Indices x:Key="Indices"
ColorIndex="1"
ColorPrefix="MyColor"
GradientIndex="1"
GradientPrefix="MyGradient" />
</Window.Resources>
<Grid x:Name="grid" DataContext="{StaticResource Indices}">
<Grid.BindingGroup>
<BindingGroup>
<BindingGroup.ValidationRules>
<wpfApplication6:ValidateMe />
</BindingGroup.ValidationRules>
</BindingGroup>
</Grid.BindingGroup>
<TextBox TextChanged="TextBox1_OnTextChanged">
<Binding Path="ColorIndex" UpdateSourceTrigger="PropertyChanged" />
</TextBox>
</Grid>
</Window>
使用 RangeAtribute :
private void test()
{
Indices indices = new Indices();
indices.ColorIndex = 20;
var validationContext = new ValidationContext(indices);
var validationResults = new List<System.ComponentModel.DataAnnotations.ValidationResult>();
var tryValidateObject = Validator.TryValidateObject(indices, validationContext, validationResults,true);
}
public class Indices
{
[Range(1, 10)]
public int ColorIndex { get; set; }
public string ColorPrefix { get; set; }
public int GradientIndex { get; set; }
public string GradientPrefix { get; set; }
}