/////////////////////////////////////////////////////////////////////////////// //Copyright (c) 2016, 北京探索者软件公司 //All rights reserved. //文件名称: BaseObjectToComboxItemConverter.cs //文件描述: 基础的转换 //创 建 者: xls //创建日期: 2018-1-30 //版 本 号:1.0.0.0 //////////////////////////////////////////////////////////////////////////////// using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Windows.Data; namespace FWindSoft.Wpf { public class BaseItemsConverter : IValueConverter { private Func m_Fun, m_TurnFun; public BaseItemsConverter() { } public BaseItemsConverter(Func fun,Func turnFun) { m_Fun = fun; m_TurnFun = turnFun; } public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { var enumerable = parameter as IEnumerable; if (enumerable == null) return value; Func tempConvert = m_Fun; if (tempConvert == null) { tempConvert = (v) => { if (v == null) return string.Empty; return v; }; } return enumerable.OfType().FirstOrDefault(i => i.ToString().Equals(tempConvert(value).ToString())); } catch (Exception ex) { throw; } return value; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { try { var enumerable = parameter as IEnumerable; if (enumerable == null) return value; Func tempConvert = m_TurnFun; if (tempConvert == null) { tempConvert = (v) => { if (v == null) return string.Empty; return v; }; } return System.Convert.ChangeType(tempConvert(value), targetType); } catch (Exception ex) { throw; } return value; } } }