赞
踩
什么是Convert ?
Convert可以将源数据和目标数据之间进行特定的转化。定义转换器,
需要继承接口IValueConverter。
public class ForeColorConverter : IValueConverter
{
//源属性传给目标属性时,调用此方法Convert
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
//目标属性传给源属性时,调用此方法ConvertBack
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
public class ColorConvert : IValueConverter { /// <summary> /// 源属性传给目标属性时,调用此方法Convert /// </summary> /// <param name="value">数据源</param> /// <param name="targetType">目标类型</param> /// <param name="parameter">不常用</param> /// <param name="culture">不常用</param> /// <returns>目标值</returns> public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) return null; //TODO:转换逻辑 int index = System.Convert.ToInt32(value); if (index == 0) return "Blue"; else if (index == 1) return "Green"; else return "Red"; } /// <summary> /// 目标属性传给源属性时,调用此方法ConvertBack /// </summary> /// <param name="value">目标值</param> /// <param name="targetType"></param> /// <param name="parameter"></param> /// <param name="culture"></param> /// <returns>数据源</returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { return null; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。