我正试图重构我的代码。目前,我的Validator类拥有10个不同的字符串,每个字符串表示不同文本框的内容。
// The string contained in the TextBox.
private string MasterPointName => null;
// Other strings...
....
// The validator.
public string this[string columnName]
{
string result = null;
get
{
switch (columnName)
{
case "MasterPointName":
if (string.IsNullOrEmpty(this.MasterPointName))
{
result = "This field must not be left empty!";
}
break;
// Other case statements...
...
}
return result;
}
}
这是上述文本框之一的XAML:
<TextBox x:Name="tbMaster" Validation.Error="ValidationError" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MasterPointName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
现在我想把我所有的弦放在一个
List<string>
private readonly List<string> points = new List<string>
{
"masterPointName",
...
};
// The validator.
public string this[string columnName]
{
string result = null;
get
{
switch (columnName)
{
case "masterPointName":
if (string.IsNullOrEmpty(this.points[0]))
{
result = "This field must not be left empty!";
}
break;
// Other case statements...
...
}
return result;
}
}
更新的XAML(使用
Path=points[0]
):
<TextBox x:Name="tbMaster" Validation.Error="ValidationError" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=points[0], ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
this
回答,但我可能遗漏了什么。我想达到的目标可能实现吗?这有什么意义吗?