gpt4 book ai didi

.net - Silverlight - INotifyDataErrorInfo 和复杂的属性绑定(bind)

转载 作者:行者123 更新时间:2023-12-04 21:24:14 25 4
gpt4 key购买 nike

我有一个实现 INotifyDataErrorInfo 的 View 模型。我将一个文本框绑定(bind)到这样的 View 模型属性之一:

<TextBox Text="{Binding SelfAppraisal.DesiredGrowth, Mode=TwoWay, ValidatesOnNotifyDataErrors=True,NotifyOnValidationError=True}"  Height="200" 
TextWrapping="Wrap"/>

数据绑定(bind)有效,但是当我添加如下验证错误时 UI 没有响应:
// validation failed
foreach (var error in vf.Detail.Errors)
{
AddError(SelfAppraisalPropertyName + "." + error.PropertyName, error.ErrorMessage);
}

运行后 GetErrors("SelfAppraisal.DesiredGrowth")在即时窗口中,我可以看到: Count = 1

[0]: "Must be at least 500 characters. You typed 4 characters."

我已确保添加错误时的连接与文本框上的绑定(bind)表达式匹配,但 UI 不会像我切换到使用复杂类型之前那样显示消息。

我究竟做错了什么?使用 INotifyDataErrorInfo 进行验证是否支持这一点?

更新

我的 View 模型,它实现了 INotifyDataErrorInfo 在添加/删除错误时确实会引发 ErrorsChanged 。
protected void RaiseErrorsChanged(string propertyName)
{
if (ErrorsChanged != null)
{
ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName));
}
}

最佳答案

TextBox正在看SelfAppraisal错误通知的对象。您似乎正在使用 SelfAppraisal 将错误添加到对象中。属性(property)。尝试将错误添加到 SelfAppraisal目的:

foreach (var error in vf.Detail.Errors)
{
SelfAppraisal.AddError(error.PropertyName, error.ErrorMessage);
}

这将在 SelfAppraisal 的实例上引发事件。属性(property)。 TextBox查找 DesiredGrowth 的错误名称,因为它绑定(bind)到该属性。

或许说明 TextBox 会有所帮助。未查看根对象是否存在属性名称为 SelfAppraisal.DesiredGrowth 的错误.

更新:使用 ViewModel 模式对您有利。在您的 VM 上创建一个属性:
public string SelfAppraisalDesiredGrowth
{
get { return SelfAppraisal != null ? SelfAppraisal.DesiredGrowth : null; }
set
{
if (SelfAppraisal == null)
{
return;
}

if (SelfAppraisal.DesiredGrowth != value)
{
SelfAppraisal.DesiredGrowth = value;
RaisePropertyChanged("SelfAppraisalDesiredGrowth");
}
}
}

绑定(bind)到这个属性:
<TextBox Text="{Binding SelfAppraisalDesiredGrowth, Mode=TwoWay, ValidatesOnNotifyDataErrors=True,NotifyOnValidationError=True}"  Height="200" TextWrapping="Wrap"/>

验证时使用 VM 属性:
// validation failed
foreach (var error in vf.Detail.Errors)
{
AddError(SelfAppraisalPropertyName + error.PropertyName, error.ErrorMessage);
}

关于.net - Silverlight - INotifyDataErrorInfo 和复杂的属性绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6416206/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com