我IDataErrorInfo
在 WPF 中用于验证,但是当属性是数组时,我可以处理错误。(在这种情况下int[] Position
)
我的代码与此类似:http ://codeblitz.wordpress.com/2009/05/08/wpf-validation-made-easy-with-idataerrorinfo/
public class Customer : IDataErrorInfo
{
public string FirstName { get; set; }
public int[] Position { get; set; }
#region IDataErrorInfo Members
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = null;
if (columName == "FirstName")
{
// Do something
}
if (columnName == "Position")
{
// Do something
}
return result;
}
}
#endregion
}
XAML
<TextBox x:Name="tbFirstName" Grid.Row="2" Grid.Column="1" Width="50" HorizontalAlignment="left"
Validation.Error="Validation_Error" MaxLength="2"
Text="{Binding UpdateSourceTrigger=PropertyChanged,
Path=FirstName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
<TextBox x:Name="tbPosition1" Grid.Row="2" Grid.Column="1" Width="50" HorizontalAlignment="left"
Validation.Error="Validation_Error" MaxLength="2"
Text="{Binding UpdateSourceTrigger=PropertyChanged,
Path=Position[0], ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
我可以毫无问题地捕获“名字”,但是如果我在 btPosition1 的文本框中进行更改,则程序不会通过该函数来处理错误。