0
我有一個類,我想在文本框中顯示List<string> Tags
。爲此,我使用了一個IValueConverter。使用IDataErrorInfo和IValueConverter進行錯誤驗證
ListToStringConverter:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var list = value as List<string>;
var returnvalue = string.Empty;
if(list != null)
list.ForEach(item => returnvalue += item + ", ");
return returnvalue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
var strValue = value as string;
if(strValue != null)
return strValue.Split(new char[] { ',' });
return null;
}
類檢驗:
public class Test : IDataErrorInfo
{
public Test()
{
Tags = new List<string>();
}
public List<string> Tags { get; set; }
string errors;
const string errorsText = "Error in Test class.";
public string Error
{
get { return errors; }
}
public string this[string propertyName]
{
get
{
errors = null;
switch (propertyName)
{
case "Tags":
if (Tags.Count <= 1)
{
errors = errorsText;
return "...more tags plz..";
}
break;
}
return null;
}
}
}
現在我想驗證標記:
<TextBox Text="{Binding Test.Tags, Converter={StaticResource ListToStringConverter}, Mode=TwoWay, ValidatesOnDataErrors=True}" />
但沒有顯示錯誤。也許是因爲轉換,但我怎麼能仍然完成驗證?
有一個愚蠢的錯誤,將對象設置爲CurrentItem,定義了EditTemplate而不是使用我使用的Object.Property的屬性名稱:-( – jwillmer 2012-08-23 08:20:40