星火直播PC
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GenericTypeConverter.cs 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Data;
  5. namespace XHZB.Desktop.Utils
  6. {
  7. public class GenericTypeConverter : IValueConverter
  8. {
  9. /// <summary>
  10. /// 正向键值对字典
  11. /// </summary>
  12. private Dictionary<string, string> Alias { get; set; }
  13. /// <summary>
  14. /// 反向键值对字典
  15. /// </summary>
  16. private Dictionary<string, string> BackAlias { get; set; }
  17. private string aliasStrTemp = "";
  18. /// <summary>
  19. /// 解析转换规则
  20. /// </summary>
  21. /// <param name="aliasStr">规则字符串</param>
  22. private void ParseAliasByStr(string aliasStr)
  23. {
  24. if (aliasStrTemp == aliasStr)
  25. {
  26. return;
  27. }
  28. aliasStrTemp = aliasStr;
  29. Alias = new Dictionary<string, string>();
  30. BackAlias = new Dictionary<string, string>();
  31. string content = aliasStr;
  32. Alias = new Dictionary<string, string>();
  33. string[] arr1 = content.Split('|');
  34. foreach (string item in arr1)
  35. {
  36. string[] arr2 = item.Split(':');
  37. string key = arr2[0];
  38. if (key == "true")
  39. {
  40. key = "True";
  41. }
  42. else if (key == "false")
  43. {
  44. key = "False";
  45. }
  46. string value = arr2[1];
  47. if (key != "other")
  48. {
  49. BackAlias.Add(value, key);
  50. }
  51. Alias.Add(key, value);
  52. }
  53. }
  54. private object ConvertCommon(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture, bool isBack)
  55. {
  56. if (value == null || string.IsNullOrEmpty(parameter.ToString()))
  57. {
  58. return null;
  59. }
  60. object ret = value;//如果没有匹配返回传入的值
  61. ParseAliasByStr(parameter.ToString());
  62. Dictionary<string, string> alias;
  63. if (isBack)
  64. {
  65. alias = BackAlias;
  66. }
  67. else
  68. {
  69. alias = Alias;
  70. }
  71. //绑定的值
  72. string bindingValue = value.ToString();
  73. if (alias.ContainsKey(bindingValue))
  74. {
  75. ret = StringToTargetType(alias[bindingValue], targetType);
  76. }
  77. else if (alias.ContainsKey("other"))
  78. {
  79. ret = StringToTargetType(alias["other"], targetType);
  80. }
  81. else if (alias.ContainsKey("else"))
  82. {
  83. ret = StringToTargetType(alias["else"], targetType);
  84. }
  85. return ret;
  86. }
  87. /// <summary>
  88. /// 字符串转换成目标类型,如需添加一个目标类型只需在该方法中添加一个类型判断之后转换
  89. /// </summary>
  90. /// <param name="strValue"></param>
  91. /// <param name="targetType"></param>
  92. /// <returns></returns>
  93. private object StringToTargetType(string strValue, Type targetType)
  94. {
  95. object ret = null;
  96. //目标类型 string
  97. if (targetType == typeof(string) || targetType == typeof(char))
  98. {
  99. ret = strValue;
  100. }
  101. //目标类型 char
  102. if (targetType == typeof(char))
  103. {
  104. if (strValue.Length == 1)
  105. {
  106. ret = strValue;
  107. }
  108. }
  109. //目标类型 int
  110. if (targetType == typeof(int))
  111. {
  112. if (int.TryParse(strValue, out int temp))
  113. {
  114. ret = temp;
  115. }
  116. else
  117. {
  118. ret = 0;
  119. }
  120. }
  121. //目标类型 double
  122. if (targetType == typeof(double))
  123. {
  124. if (double.TryParse(strValue, out double temp))
  125. {
  126. ret = temp;
  127. }
  128. else
  129. {
  130. ret = 0;
  131. }
  132. }
  133. //目标类型 float
  134. if (targetType == typeof(float))
  135. {
  136. if (float.TryParse(strValue, out float temp))
  137. {
  138. ret = temp;
  139. }
  140. else
  141. {
  142. ret = 0;
  143. }
  144. }
  145. //目标类型 decimal
  146. if (targetType == typeof(decimal))
  147. {
  148. if (decimal.TryParse(strValue, out decimal temp))
  149. {
  150. ret = temp;
  151. }
  152. else
  153. {
  154. ret = 0;
  155. }
  156. }
  157. //目标类型 bool? bool
  158. if (targetType == typeof(bool?) || targetType == typeof(bool))
  159. {
  160. if (bool.TryParse(strValue, out bool temp))
  161. {
  162. ret = temp;
  163. }
  164. else
  165. {
  166. ret = false;
  167. }
  168. }
  169. //目标类型 Visibility
  170. if (targetType == typeof(Visibility))
  171. {
  172. switch (strValue.ToLower())
  173. {
  174. case "collapsed":
  175. ret = Visibility.Collapsed;
  176. break;
  177. case "hidden":
  178. ret = Visibility.Hidden;
  179. break;
  180. case "visible":
  181. ret = Visibility.Visible;
  182. break;
  183. default:
  184. ret = Visibility.Visible;
  185. break;
  186. }
  187. }
  188. return ret;
  189. }
  190. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  191. {
  192. return ConvertCommon(value, targetType, parameter, culture, false);
  193. }
  194. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  195. {
  196. return ConvertCommon(value, targetType, parameter, culture, true);
  197. }
  198. }
  199. }