备份一个个人用的WPF万能转换器

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Data;

namespace Health.Client.Resources.Converters
{
public class CommonCoverter : IValueConverter
{
/// 参数必填,为键值对,用空格分隔,不限键值对个数 格式:key1,value1 key2,value2 ....[other,defultValue]
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{

#region 参数转dictionary
Dictionary<string, string> PStrDic = new Dictionary<string, string>();
object result = null;
var PStr = (string)parameter;
if (!(value.GetType() == typeof(DateTime) && targetType==typeof(string)))
{
foreach (Match m in Regex.Matches(PStr, @"\w+\,\w+"))
{
var p_key = Regex.Match(m.Value, @"^\w+").Value;
var p_value = Regex.Match(m.Value, @"\w+$").Value;
PStrDic.Add(p_key, p_value);
};

}
#endregion
// to visibility
if (targetType == typeof(Visibility) || targetType == typeof(Visibility?))
{
if (PStrDic.ContainsKey(value.ToString().ToLower()))
{
var temp = PStrDic[value.ToString().ToLower()].ToLower();
result = temp == "visible" ? Visibility.Visible
: temp == "hidden" ? Visibility.Hidden
: temp == "collapsed" ? Visibility.Collapsed
: Binding.DoNothing;
}
else
{
result = PStrDic.Any(t=>t.Key.ToLower() == "other")?PStrDic.Single(t=>t.Key.ToLower()=="other").Value:(object) Visibility.Collapsed;

}
}
// to int
if (targetType == typeof(int) || targetType == typeof(int?))
{
if (PStrDic.ContainsKey(value.ToString().ToLower()))
{
var temp = PStrDic[value.ToString().ToLower()].ToLower();
result = int.Parse(temp);
}
else
{
result = PStrDic.Any(t => t.Key.ToLower() == "other") ? PStrDic.Single(t => t.Key.ToLower() == "other").Value : (object)0;
}
}
// to bool
if (targetType == typeof(bool?) || targetType == typeof(bool))
{
if (PStrDic.ContainsKey(value.ToString().ToLower()))
{
var temp = PStrDic[value.ToString().ToLower()].ToLower();
result = bool.Parse(temp);
}
else
{
result = PStrDic.Any(t => t.Key.ToLower() == "other") ? PStrDic.Single(t => t.Key.ToLower() == "other").Value : (object)false;
}
}
// to string
if (targetType == typeof(string) )
{
if (PStrDic.ContainsKey(value.ToString().ToLower()))
{
var temp = PStrDic[value.ToString().ToLower()].ToLower();
result = temp;
}
else
{
result = PStrDic.Any(t => t.Key.ToLower() == "other") ? PStrDic.Single(t => t.Key.ToLower() == "other").Value : (object)"";
}
}
//datetime
if (targetType == typeof(string) && value.GetType() == typeof(DateTime))
{
var dt = (DateTime)value;
if (dt==DateTime.MinValue)
{
result = "";
}
else
{
result = PStrDic.Any(t => t.Key.ToLower() == "other") ? PStrDic.Single(t => t.Key.ToLower() == "other").Value : (object)dt;
}
}

return result ?? Binding.DoNothing;

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
#region 参数转dictionary
Dictionary<string, string> PStrDic = new Dictionary<string, string>();
object result = null;
var PStr = (string)parameter;
foreach (Match m in Regex.Matches(PStr, @"\w+\,\w+"))
{
var p_key = Regex.Match(m.Value, @"^\w+").Value;
var p_value = Regex.Match(m.Value, @"\w+$").Value;
PStrDic.Add(p_key, p_value);
};

#endregion
PStrDic = PStrDic.Where(t => t.Key.ToLower() != "other").ToDictionary(t=>t.Key,t=>t.Value);
//bool
if (targetType == typeof(bool) || targetType == typeof(bool?))
{
if (PStrDic.ContainsValue(value.ToString().ToLower()))
{
result = bool.Parse(PStrDic.FirstOrDefault(t => t.Value.ToLower() == value.ToString().ToLower()).Key);

}
else
{
result = null;
}
}

//int
if (targetType == typeof(int) || targetType == typeof(int?))
{
if (PStrDic.ContainsValue(value.ToString().ToLower()))
{
result = int.Parse(PStrDic.FirstOrDefault(t => t.Value.ToLower() == value.ToString().ToLower()).Key);

}
else
{
result = null;
}
}
//string
if (targetType == typeof(string) )
{
if (PStrDic.ContainsValue(value.ToString().ToLower()))
{
result = PStrDic.FirstOrDefault(t => t.Value.ToLower() == value.ToString().ToLower()).Key;

}
else
{
result = null;
}

}
//datetime
if (targetType == typeof(DateTime))
{
result = value??DateTime.MinValue;
}
return result ?? Binding.DoNothing;
}
}
}

猜你喜欢

转载自www.cnblogs.com/nocanstillbb/p/9844231.html