C# WPF、文本框验证和仅绑定列表中的一项<;字符串>;
我正试图重构我的代码。目前,我的Validator类拥有10个不同的字符串,每个字符串表示不同文本框的内容 下面是我的C#代码片段(它可以正常工作): 这是上述文本框之一的XAML:C# WPF、文本框验证和仅绑定列表中的一项<;字符串>;,c#,wpf,validation,data-binding,C#,Wpf,Validation,Data Binding,我正试图重构我的代码。目前,我的Validator类拥有10个不同的字符串,每个字符串表示不同文本框的内容 下面是我的C#代码片段(它可以正常工作): 这是上述文本框之一的XAML: <TextBox x:Name="tbMaster" Validation.Error="ValidationError" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MasterPointName, ValidatesOnDataErro
<TextBox x:Name="tbMaster" Validation.Error="ValidationError" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MasterPointName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
提到答案,我以为这会奏效,但我可能遗漏了什么。我想达到的目标可能实现吗?这有什么意义吗?您只能绑定属性。将您的列表设置为属性ID您尝试将其设置为
公共可观察集合点{get;set;}
?我认为存在一些沟通错误。我不明白答案怎么说你可以在正规场上绑定?我的意思是,您必须将列表声明为公共列表点{get;set;}
,否则WPF不能执行类似{Binding points[0]}
的操作,例如,您不能绑定到类似私有字符串母节点的东西上你也不能绑定像私有列表点这样的东西不,您只需要添加额外的{get;}
。这应该做到公共列表点{get;}=new List{“masterPointName”,…}代码>
<TextBox x:Name="tbMaster" Validation.Error="ValidationError" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=MasterPointName, ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />
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;
}
}
<TextBox x:Name="tbMaster" Validation.Error="ValidationError" Text="{Binding UpdateSourceTrigger=PropertyChanged, Path=points[0], ValidatesOnDataErrors=true, NotifyOnValidationError=true}" />