|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220 |
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Data;
-
- namespace XHZB.Desktop.Utils
- {
- public class GenericTypeConverter : IValueConverter
- {
- /// <summary>
- /// 正向键值对字典
- /// </summary>
- private Dictionary<string, string> Alias { get; set; }
-
- /// <summary>
- /// 反向键值对字典
- /// </summary>
- private Dictionary<string, string> BackAlias { get; set; }
-
- private string aliasStrTemp = "";
-
- /// <summary>
- /// 解析转换规则
- /// </summary>
- /// <param name="aliasStr">规则字符串</param>
- private void ParseAliasByStr(string aliasStr)
- {
- if (aliasStrTemp == aliasStr)
- {
- return;
- }
-
- aliasStrTemp = aliasStr;
- Alias = new Dictionary<string, string>();
- BackAlias = new Dictionary<string, string>();
-
- string content = aliasStr;
-
- Alias = new Dictionary<string, string>();
- string[] arr1 = content.Split('|');
- foreach (string item in arr1)
- {
- string[] arr2 = item.Split(':');
- string key = arr2[0];
- if (key == "true")
- {
- key = "True";
- }
- else if (key == "false")
- {
- key = "False";
- }
- string value = arr2[1];
- if (key != "other")
- {
- BackAlias.Add(value, key);
- }
-
- Alias.Add(key, value);
- }
- }
-
- private object ConvertCommon(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture, bool isBack)
- {
- if (value == null || string.IsNullOrEmpty(parameter.ToString()))
- {
- return null;
- }
-
- object ret = value;//如果没有匹配返回传入的值
-
- ParseAliasByStr(parameter.ToString());
- Dictionary<string, string> alias;
- if (isBack)
- {
- alias = BackAlias;
- }
- else
- {
- alias = Alias;
- }
-
- //绑定的值
- string bindingValue = value.ToString();
-
- if (alias.ContainsKey(bindingValue))
- {
- ret = StringToTargetType(alias[bindingValue], targetType);
- }
- else if (alias.ContainsKey("other"))
- {
- ret = StringToTargetType(alias["other"], targetType);
- }
- else if (alias.ContainsKey("else"))
- {
- ret = StringToTargetType(alias["else"], targetType);
- }
-
- return ret;
- }
-
- /// <summary>
- /// 字符串转换成目标类型,如需添加一个目标类型只需在该方法中添加一个类型判断之后转换
- /// </summary>
- /// <param name="strValue"></param>
- /// <param name="targetType"></param>
- /// <returns></returns>
- private object StringToTargetType(string strValue, Type targetType)
- {
- object ret = null;
- //目标类型 string
- if (targetType == typeof(string) || targetType == typeof(char))
- {
- ret = strValue;
- }
- //目标类型 char
- if (targetType == typeof(char))
- {
- if (strValue.Length == 1)
- {
- ret = strValue;
- }
- }
- //目标类型 int
- if (targetType == typeof(int))
- {
- if (int.TryParse(strValue, out int temp))
- {
- ret = temp;
- }
- else
- {
- ret = 0;
- }
- }
- //目标类型 double
- if (targetType == typeof(double))
- {
- if (double.TryParse(strValue, out double temp))
- {
- ret = temp;
- }
- else
- {
- ret = 0;
- }
- }
- //目标类型 float
- if (targetType == typeof(float))
- {
- if (float.TryParse(strValue, out float temp))
- {
- ret = temp;
- }
- else
- {
- ret = 0;
- }
- }
- //目标类型 decimal
- if (targetType == typeof(decimal))
- {
- if (decimal.TryParse(strValue, out decimal temp))
- {
- ret = temp;
- }
- else
- {
- ret = 0;
- }
- }
- //目标类型 bool? bool
- if (targetType == typeof(bool?) || targetType == typeof(bool))
- {
- if (bool.TryParse(strValue, out bool temp))
- {
- ret = temp;
- }
- else
- {
- ret = false;
- }
- }
-
- //目标类型 Visibility
- if (targetType == typeof(Visibility))
- {
- switch (strValue.ToLower())
- {
- case "collapsed":
- ret = Visibility.Collapsed;
- break;
-
- case "hidden":
- ret = Visibility.Hidden;
- break;
-
- case "visible":
- ret = Visibility.Visible;
- break;
-
- default:
- ret = Visibility.Visible;
- break;
- }
- }
- return ret;
- }
-
- public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- return ConvertCommon(value, targetType, parameter, culture, false);
- }
-
- public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
- {
- return ConvertCommon(value, targetType, parameter, culture, true);
- }
- }
- }
|