gpt4 book ai didi

c# - IDataErrorInfo问题

转载 作者:行者123 更新时间:2023-12-03 10:55:31 25 4
gpt4 key购买 nike

我的viewmodel类中有两个属性[Say Size,StrVal]。
约束之一是StrVal的长度应小于或等于Size。
此约束适用于IDataErrorInfo索引器。

public string this[string propertyName]
{

get
{
string msg = null; ;
switch (propertyName)
{
....

case "StrVal":
{
if (this.StrVal.Length > this.SizeOfStringVal)
{
msg = "Size of entered value is greater than the size";
}
}
break;

.........

}
return msg;
}
}

现在考虑以下情况
Size = 5;
StrVal = "ABCDEF" ; // length = 6 > Size
"Error message is generated"
Size = 7 // length of StrVal is less than 7

但是在视觉上仍然显示错误情况,直到我以编程方式触发“StrVal”属性的propertyChanged事件为止。因此,我必须使用以下代码。
public int? Size
{
get
{
return this.size;
}
set
{
if (value == this.Size)
{
return;
}
this.size = value;
this.OnPropertyChanged("StrVal");
}
}

请告知这是否是解决问题的理想方法。
问候,
阿尼类

最佳答案

是的,这是IDataErrorInfo的工作方式,它只会在发生属性更改通知时查询验证错误。因此,理想情况下,您的Size属性应如下所示:

public int? Size
{
get
{
return this.size;
}
set
{
if (value == this.Size)
{
return;
}
this.size = value;
this.OnPropertyChanged("Size");
this.OnPropertyChanged("StrVal");
}
}

即使您可能没有对size属性进行任何验证,您仍应(作为“最佳实践”问题)发送属性更改通知。

关于c# - IDataErrorInfo问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14777010/

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