- 要检索错误信息的属性名称
- Null
- String.Empty
澄清:我不是在问如何实现 GetErrors 方法(我只是测试了 null 和 empty 两种情况)。问题更多地是为了理解为什么 WPF 框架有时会使用 null,有时会使用空字符串来调用此方法(我在我的应用程序中遇到了这两种情况)。如果意图是询问未绑定到特定属性的错误,请为什么需要使用两种不同的调用值,而只使用其中一种就足够了?
当调用程序想要知道实体所有参数的错误,而不是特定参数的错误时,您必须预期null
和string.Empty
。因此,您不应区分null
和string.Empty
。
所以应该这样处理:
if(string.IsNullOrEmpty(propertyName))
{
// return all errors
}
else
{
// return the error for the parameter 'propertyName'
}
WPF会调用InotifyDataErrorInfo.GetErrors(null/string.Empty)方法来获取“整个视图模型”的错误。所有具有数据上下文或绑定到具有“整个视图模型错误”的视图模型的控件都将使用错误模板呈现。例如,您有一个名为Credentials的视图模型,其中包含两个属性:UserName和Password。您可以实现如下:
IEnumerable InotifyDataErrorInfo.GetErrors(string propertyName)
{
if (UserNames.Length == 0)
{
if (string.IsNullOrEmpty(propertyName))
{
return "Some credentials component is wrong.";
}
else if (propertyName == "UserNames")
{
return "User name is required field.";
}
}
}
然而,在大多数情况下,当string.IsNullOrEmpty(propertyName)时,您可能会返回null。
当绑定引用无属性时,例如默认Path,则会得到空参数:
最初的回答: You get the null argument when the binding refers to no property, as with the default Path:
DataContext="{Binding ValidatesOnNotifyDataErrors=True}"
DataContext="{Binding Path=., ValidatesOnNotifyDataErrors=True}"
Tag="{Binding Path=DataContext, RelativeSource={RelativeSource Self}, ValidatesOnNotifyDataErrors=True}"
关于string.Empty,在内部BindingExpression.UpdateNotifyDataErrors方法的框架中已经明确说明:
最初的回答
List<object> propertyErrors = GetDataErrors(indei, propertyName);
List<object> valueErrors = GetDataErrors(dataErrorValue, String.Empty);
List<object> errors = MergeErrors(propertyErrors, valueErrors);
警告: 注意上面的聚合操作,因此如果您使用string.IsNullOrEmpty来验证框架是否要求实体级错误,并且无论"null" vs. string.Emtpy都返回相同的错误,您可能会在UI中获得重复的消息(一个为“null”,另一个为string.Empty)。至少在今天的.NET框架版本中是这样。
最初的回答: 警告:请注意上述聚合操作,如果您使用string.IsNullOrEmpty来验证框架是否请求实体级别的错误,并且无论是“null”还是“string.Emtpy”,都返回相同的错误,那么您可能会在UI中看到重复的消息(一个为“null”,另一个为string.Empty)。至少在当前的.NET框架版本中是如此。
INotifyDataErrorInfo.GetErrors 什么时候使用 null 或 String.empty 调用?
如果您想验证单个属性,您将调用
RaiseEvent ErrorsChanged(Me, New DataErrorsChangedEventArgs("PropertyName"))
从该属性的setter方法中返回。
如果您想验证整个实体,您将调用
RaiseEvent ErrorsChanged(Me, New DataErrorsChangedEventArgs(String.Empty))
从某个“中心”位置,例如保存按钮点击或窗口关闭。
null
和string.Empty
? - DHN