WPF-通用转换器

/// <summary>
    /// 一个通用的类型转换器,可以提供更多转换控制参数
    /// </summary>
    public class GenericTypeConverter: IValueConverter
    {
        /// <summary>
        /// 是否反转转换源参数值
        /// 仅对bool类型的值有效
        /// </summary>
        private bool IsReverse { get; set; }

        /// <summary>
        /// 用于将转换结果映射为其它字符串
        /// 例如:Alias=True:是|False:否
        /// </summary>
        private Dictionary<object, string> Alias { get; set; }

        /// <summary>
        /// 解析转换参数
        /// </summary>
        private void AnalyseConvertParameter(string convertParameter)
        {
            /*设置参数默认值*/
            IsReverse = false;
            Alias = null;

            if (!string.IsNullOrEmpty(convertParameter))
            {
                var pkvs = convertParameter.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var pkv in pkvs)
                {
                    var pkvo = pkv.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    if (pkvo.Length != 2)
                        throw new NotSupportedException("不支持设置:" + pkv);
                    var pk = pkvo[0].Trim();
                    var pv = pkvo[1].Trim();

                    switch (pk)
                    {
                        case "IsReverse":
                            bool b;
                            if (!bool.TryParse(pv, out b))
                                throw new NotSupportedException("参数取值错误:" + pkv);
                            else
                                IsReverse = b;
                            break;
                        case "Alias":
                            {
                                Alias = new Dictionary<object, string>();
                                var dfkvs = pv.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
                                foreach (var dfkv in dfkvs)
                                {
                                    var dfkvo = dfkv.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                                    if (dfkvo.Length != 2)
                                        throw new NotSupportedException("不支持设置:" + dfkvo);
                                    var dfk = dfkvo[0].Trim();
                                    var dfv = dfkvo[1].Trim();

                                    object oKey = null;
                                    int i;
                                    if (dfk.Equals("true", StringComparison.OrdinalIgnoreCase))
                                        oKey = true;
                                    else if (dfk.Equals("false", StringComparison.OrdinalIgnoreCase))
                                        oKey = false;
                                    else if (dfk.Equals("other", StringComparison.OrdinalIgnoreCase))
                                        oKey = "other";
                                    else if (int.TryParse(dfk, out i))
                                        oKey = i;
                                    else
                                        throw new NotSupportedException("参数取值错误:" + dfkv);

                                    Alias[oKey] = dfv;
                                }
                            }
                            break;
                        default:
                            throw new NotSupportedException("不支持的参数名:" + pk);
                    }
                }
            }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            AnalyseConvertParameter(parameter as string);

            try
            {
                var sourceType = value.GetType();
                if (IsReverse && sourceType == typeof(bool))
                    value = !((bool)value);

                if (targetType == typeof(string))
                {
                    if (Alias != null && Alias.ContainsKey(value))
                        return Alias[value];
                    else if (Alias != null && Alias.ContainsKey("other"))
                        return Alias["other"];
                    else
                        return value == null ? "" : value.ToString();
                }
                if (targetType == typeof(bool))
                {
                    if (sourceType == typeof(Visibility))
                        return (Visibility)value == Visibility.Visible;
                }
                else if (targetType.IsEnum)
                {
                    if (sourceType == typeof(bool) && targetType == typeof(Visibility))
                    {
                        return (bool)value ? Visibility.Visible : Visibility.Collapsed;
                    }
                    else if (sourceType == typeof(int) && Alias != null)
                    {
                        return Enum.Parse(targetType, Alias[value], false);
                    }
                    else
                    {
                        return Enum.Parse(targetType, value.ToString(), true);
                    }
                }
                else
                {
                    return System.Convert.ChangeType(value, targetType);
                }

                return System.Convert.ChangeType(value, targetType);
            }
            catch
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return Convert(value, targetType, parameter, culture);
        }
    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值