/////////////////////////////////////////////////////////////////////////////// //Copyright (c) 2016, 北京探索者软件公司 //All rights reserved. //文件名称: BoolToBoolItemConverter.cs //文件描述: 布尔转换成BoolItem //创 建 者: xls //创建日期: 2018-1-18 //版 本 号:1.0.0.0 //////////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; namespace FWindSoft.Wpf { public class BoolToBoolItemConverter: IValueConverter { public static BoolToBoolItemConverter Instance = new BoolToBoolItemConverter(); public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Type sourceType = value.GetType(); if (sourceType!=typeof(bool)) return value; var enumerable = parameter as IEnumerable; if (enumerable == null) return value; var items = enumerable.OfType(); return items.FirstOrDefault(i => i.Value.Equals(System.Convert.ChangeType(value, sourceType))); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { BoolItem item = value as BoolItem; if (item == null) return value; return item.Value; } } }