在我的项目中,对于验证,我使用了 IDataErrorInfor,在 xaml 代码中我提到了文本框的 NotifyfyOvalidationError=true。
所有验证都正确执行,但唯一的问题是,在我看来,它没有将错误模板显示为跨越 TextBox 和工具提示的红线,我想显示它以观察此 TextBox 包含错误..
同样的想法适用于所有其他文本框,另一件事是这个文本框的验证是工作形式的视图模型,无论我在我的代码中设置什么..
xml:
<TextBox Margin="0,7" Text="{Binding Path=Address.AddressLines, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" Width="200" HorizontalAlignment="Left" VerticalAlignment="Stretch" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
查看型号:
区域 IDataErrorInfo 成员
string IDataErrorInfo.Error { get { return null; } }
string IDataErrorInfo.this[string propertyName]
{
get { return this.GetValidationError(propertyName); }
}
#endregion // IDataErrorInfo Members
#region Validation
/// <summary>
/// Returns true if this object has no validation errors.
/// </summary>
public bool IsValid
{
get
{
foreach (string property in ValidatedProperties)
if (GetValidationError(property) != null)
return false;
return true;
}
}
static readonly string[] ValidatedProperties =
{
"Address",
};
string GetValidationError(string propertyName)
{
if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
return null;
string error = null;
switch (propertyName)
{
case "Address":
error = this.ValidateAddressLine();
break;
default:
Debug.Fail("Unexpected property being validated on School: " + propertyName);
break;
}
return error;
}
string ValidateAddressLine()
{
if (IsStringMissing(this.Address.AddressLines))
{
return "Enter Address.";
}
return null;
}
static bool IsStringMissing(string value)
{
return
String.IsNullOrEmpty(value) ||
value.Trim() == String.Empty;
}
#endregion // Validation
任何人都可以找到我的问题的解决方案..