【发布时间】:2012-11-07 05:02:29
【问题描述】:
在我的项目中,我使用了 IDataErrorInfor 进行验证,在 xaml 代码中我提到了文本框的 NotifyfyOvalidationError= true。
所有验证都正确执行,但唯一的问题是,在我看来,它没有将错误模板显示为跨 TextBox 和工具提示的红线,我想显示它以观察此 TextBox 包含错误..
同样的想法适用于所有其他文本框,另一件事是,无论我在代码中设置什么,这个文本框的验证都是从视图模型中工作的。
Xaml:
<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
任何人都可以解决我的问题..
【问题讨论】:
标签: wpf validation xaml mvvm idataerrorinfo