GeneralConverter.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Windows.Data;
  7. using System.Windows.Media;
  8. using System.Windows;
  9. using Aitex.Core.RT.Log;
  10. namespace Aitex.Core.UI.Converters
  11. {
  12. public class LineColorConverter : IValueConverter
  13. {
  14. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  15. {
  16. try
  17. {
  18. Color color = (Color)value;
  19. return new SolidColorBrush(color);
  20. }
  21. catch (Exception ex)
  22. {
  23. LOG.Write(ex);
  24. }
  25. return Brushes.Black;
  26. }
  27. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  28. {
  29. try
  30. {
  31. Brush brush = (SolidColorBrush)value;
  32. Color color = (Color)ColorConverter.ConvertFromString(brush.ToString());
  33. return color;
  34. }
  35. catch (Exception ex)
  36. {
  37. LOG.Write(ex);
  38. }
  39. return Colors.Black;
  40. }
  41. }
  42. public class enum2BoolConverter : IValueConverter
  43. {
  44. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  45. {
  46. try
  47. {
  48. if (0 == (int)value)
  49. return true;
  50. return false;
  51. }
  52. catch (Exception ex)
  53. {
  54. LOG.Write(ex);
  55. }
  56. return false;
  57. }
  58. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  59. {
  60. return null;
  61. }
  62. }
  63. public class bool2VisibilityConverter : IValueConverter
  64. {
  65. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  66. {
  67. try
  68. {
  69. if ((bool)value)
  70. return Visibility.Visible;
  71. return Visibility.Hidden;
  72. }
  73. catch (Exception ex)
  74. {
  75. LOG.Write(ex);
  76. }
  77. return Visibility.Hidden;
  78. }
  79. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  80. {
  81. return null;
  82. }
  83. }
  84. public class bool2VisibilityConverter2 : IValueConverter
  85. {
  86. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  87. {
  88. try
  89. {
  90. if ((bool)value)
  91. return Visibility.Visible;
  92. return Visibility.Collapsed;
  93. }
  94. catch (Exception ex)
  95. {
  96. LOG.Write(ex);
  97. }
  98. return Visibility.Collapsed;
  99. }
  100. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  101. {
  102. return null;
  103. }
  104. }
  105. public class Visibility2boolConverter : IValueConverter
  106. {
  107. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  108. {
  109. try
  110. {
  111. Visibility vi = (Visibility)value;
  112. if (vi == Visibility.Visible) return true;
  113. return false;
  114. }
  115. catch (Exception ex)
  116. {
  117. LOG.Write(ex);
  118. }
  119. return true;
  120. }
  121. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  122. {
  123. try
  124. {
  125. bool isChecked = (bool)value;
  126. if (isChecked) return Visibility.Visible;
  127. return Visibility.Hidden;
  128. }
  129. catch (Exception ex)
  130. {
  131. LOG.Write(ex);
  132. }
  133. return Visibility.Visible;
  134. }
  135. }
  136. public class RolloverDataTimeConverter : IValueConverter
  137. {
  138. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  139. {
  140. if (value != null)
  141. {
  142. DateTime dt;
  143. var isSucc = DateTime.TryParse(value.ToString(), out dt);
  144. if (isSucc)
  145. return string.Format("{0}{1}{2}", " 【", dt.ToString("yyyy/MM/dd HH:mm:ss"), "】 ");
  146. }
  147. return "N/A";
  148. }
  149. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  150. {
  151. return null;
  152. }
  153. }
  154. public class BoolToValueConverter : IValueConverter
  155. {
  156. public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  157. {
  158. if (value == null) return null;
  159. return ((bool)value) ? parameter : DependencyProperty.UnsetValue;
  160. }
  161. public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  162. {
  163. if (value == null) return null;
  164. return object.Equals(value, parameter);
  165. }
  166. }
  167. public class RadioBoolToIntConverter : IValueConverter
  168. {
  169. public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  170. {
  171. int integer = (int)value;
  172. if (integer == int.Parse(parameter.ToString()))
  173. return true;
  174. else
  175. return false;
  176. }
  177. public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  178. {
  179. return parameter;
  180. }
  181. }
  182. public class FlowConverterForWeiQiMultiBinding : IMultiValueConverter
  183. {
  184. #region IMultiValueConverter 成员
  185. public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  186. {
  187. string str_Result = string.Empty;
  188. bool isOpen = false;
  189. for (int i = 0; i < values.Length; i++)
  190. {
  191. bool.TryParse(values.GetValue(i) + "", out isOpen);
  192. if (isOpen == false)
  193. return isOpen;
  194. }
  195. return true;
  196. }
  197. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  198. {
  199. throw new NotImplementedException();
  200. }
  201. #endregion
  202. }
  203. /// <summary>
  204. /// 其中有一项为true就是true
  205. /// </summary>
  206. public class FlowConverterForAllTrueMultiBinding : IMultiValueConverter
  207. {
  208. #region IMultiValueConverter 成员
  209. public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  210. {
  211. string str_Result = string.Empty;
  212. bool isOpen = false;
  213. for (int i = 0; i < values.Length; i++)
  214. {
  215. bool.TryParse(values.GetValue(i) + "", out isOpen);
  216. if (isOpen == true)
  217. return isOpen;
  218. }
  219. return false;
  220. }
  221. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  222. {
  223. throw new NotImplementedException();
  224. }
  225. #endregion
  226. }
  227. /// <summary>
  228. /// 第一项false则关闭,后面的只要有true就打开
  229. /// </summary>
  230. public class FlowConverterFirstFalseMultiBinding : IMultiValueConverter
  231. {
  232. #region IMultiValueConverter 成员
  233. public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  234. {
  235. if (values.Length < 2) return false;
  236. bool isOpen = false;
  237. bool.TryParse(values.GetValue(0) + "", out isOpen);
  238. if (!isOpen) return false;
  239. isOpen = false;
  240. for (int i = 1; i < values.Length; i++)
  241. {
  242. bool.TryParse(values.GetValue(i) + "", out isOpen);
  243. if (isOpen == true)
  244. return true;
  245. }
  246. return false;
  247. }
  248. public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
  249. {
  250. throw new NotImplementedException();
  251. }
  252. #endregion
  253. }
  254. }