public class AddressConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { string result = string.Empty; if (value != null) { string[] CustomCode = value.ToString().Split(','); for (int i = 0; i < CustomCode.Count(); i++) { var tempCustom = CodeListHelper.AllAddressList.Where(c => c.SelectValue == CustomCode[i]).FirstOrDefault(); if (i == 0) { if (tempCustom == null) { result = CustomCode[i]; } else { result = tempCustom.DisplayValue; } } else { if (tempCustom == null) { result += "," + CustomCode[i]; } else { result += "," + tempCustom.DisplayValue; } } } return result; } return result; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
上面是转换活的例子,下面是转换死的例子
public class CarTypeConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value != null) { var tempCustom = CodeListHelper.DispatchTypeList.Where(c => c.SelectValue == value.ToString()).FirstOrDefault(); if (tempCustom != null) { return tempCustom.DisplayValue; } } return value; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } }
说这个意思。
本文介绍了如何在WPF中使用自定义值转换器(IValueConverter)进行数据绑定的转换,通过两个具体示例,一个是地址转换器(AddressConverter),另一个是车型转换器(CarTypeConverter),展示了从复杂数据类型到简单显示文本的转换过程。
977

被折叠的 条评论
为什么被折叠?



