【发布时间】:2017-06-06 23:27:16
【问题描述】:
更新:我通过改进我的 switch 语句解决了这个问题。使用了nameof!
我正在尝试验证来自文本框的一组用户输入。
我的班级设置了界面。摘录如下:
public class PatientValidation : INotifyPropertyChanged, IDataErrorInfo
{
private string _id;
private string _fname;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string p)
{
PropertyChangedEventHandler ph = PropertyChanged;
if (ph != null)
{
ph(this, new PropertyChangedEventArgs(p));
}
}
public string Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
public string Fname
{
get
{
return _fname;
}
set
{
_fname = value;
}
}
以及根据用户输入返回错误消息的 switch 语句:
public string this[string PropertyName]
{
get
{
string result = null;
switch (PropertyName)
{
case "Id":
if (string.IsNullOrEmpty(Id))
result = "ID number is required.";
break;
case "fname":
if (string.IsNullOrEmpty(Fname))
result = "First name is required.";
break;
}
return result;
}
}
我在 XAML 中的相关代码:
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
<TextBox x:Name="textBox_IDNumber" Text="{Binding Id, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
<TextBox x:Name="textBox_FirstName" Text="{Binding Fname, Mode=TwoWay, ValidatesOnDataErrors=True"}/>
这是我遇到的问题:只有第一个文本框 (ID) 被正确验证并显示错误工具提示。没有其他文本框。 不同的绑定和触发器并没有解决这个问题。任何帮助将非常感激。
【问题讨论】:
标签: c# wpf idataerrorinfo