1.作用:可以将源数据和目标数据之间进行特定的转化,
2.定义转换器,需要继承接口IValueConverter
[ValueConversion(typeof(int), typeof(string))]
public class ForeColorConverter : IValueConverter
{
//源属性传给目标属性时,调用此方法ConvertBack
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int c = System.Convert.ToInt32(parameter);
if (value == null)
throw new ArgumentNullException("value can not be null");
int index = System.Convert.ToInt32(value);
if (index == 0)
return "Blue";
else if (index == 1)
return "Red";
else
return "Green";
}
//目标属性传给源属性时,调用此方法ConvertBack