【发布时间】:2014-01-30 08:49:11
【问题描述】:
目前我正在使用 IDataErrorInfo 但问题是我想使用 对所有文本框进行此精确代码验证,现在如果我为 textBox1 框输入无效数据,所有 textBox2 也 都会出现红色边框。
我不想为所有文本框使用特定大小写(我有更多的 15 个...),因为所有文本框的验证中的所有内容都应该相同。应该怎么分?
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
var error = "";
switch (columnName)
{
case "ListItem":
if (ListItem != null)
{
var list = new List<String> { "FirstName", "LastName", "BusinessItem", "BusinessItems" };
string value = ListItem.Trim();
var isValid = true;
if (!string.IsNullOrEmpty(value))
{
isValid = list.Contains(value);
if (!isValid)
{
error = "Please enter either FirstName, LastName, BusinessItem, or BusinessItems";
}
}
}
break;
}
return error;
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
Xaml
<TextBox name="textBox1" Text="{Binding Path=ListItem,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
HorizontalAlignment="Center" VerticalAlignment="Center" Width="200"/>
<TextBox name="textBox2" Text="{Binding Path=ListItem,UpdateSourceTrigger=PropertyChanged,ValidatesOnDataErrors=True}"
HorizontalAlignment="Center" VerticalAlignment="Center" Width="200"/>
【问题讨论】:
-
您实际上是否将所有文本框绑定到同一个属性?