ElementValueProvider.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using FWindSoft.Data;
  9. using FWindSoft.SystemExtensions;
  10. namespace FWindSoft.Wpf.History
  11. {
  12. /// <summary>
  13. /// 元素值抽象类
  14. /// </summary>
  15. public abstract class ElementValueProvider:IValueProvider
  16. { /*
  17. 为统一接口,不把相关control 参数放在 GetValue和SetValue方法中
  18. */
  19. public ElementValueProvider()
  20. {
  21. RefControlType=GetControlType();
  22. }
  23. public void SetAdapterControl(FrameworkElement element)
  24. {
  25. RefControl = element;
  26. }
  27. public Type RefControlType { get; private set; }
  28. /// <summary>
  29. /// 关联控件
  30. /// </summary>
  31. public FrameworkElement RefControl { get; private set; }
  32. /// <summary>
  33. /// 获取控件值
  34. /// </summary>
  35. /// <returns></returns>
  36. public abstract object GetValue();
  37. /// <summary>
  38. /// 设置控件值
  39. /// </summary>
  40. /// <param name="value"></param>
  41. public abstract void SetValue(object value);
  42. /// <summary>
  43. /// 设置控件关联值
  44. /// </summary>
  45. protected abstract Type GetControlType();
  46. /// <summary>
  47. /// 判断是否是适配类型
  48. /// </summary>
  49. /// <param name="type"></param>
  50. /// <returns></returns>
  51. public bool IsAdapterType(Type type)
  52. {
  53. return RefControlType == type;
  54. }
  55. public ElementValueProvider Clone()
  56. {
  57. return this.MemberwiseClone() as ElementValueProvider;
  58. }
  59. }
  60. /// <summary>
  61. /// 文本框适配
  62. /// </summary>
  63. public class TextBoxValueProvider : ElementValueProvider
  64. {
  65. public override object GetValue()
  66. {
  67. TextBox textBox = RefControl as TextBox;
  68. string str = string.Empty;
  69. if (textBox != null)
  70. {
  71. str = textBox.Text;
  72. }
  73. return str;
  74. }
  75. public override void SetValue(object value)
  76. {
  77. TextBox textBox = RefControl as TextBox;
  78. object str = value??string.Empty;
  79. if (textBox != null)
  80. {
  81. textBox.Text= str.ToString();
  82. }
  83. }
  84. protected override Type GetControlType()
  85. {
  86. return typeof(TextBox);
  87. }
  88. }
  89. /// <summary>
  90. /// 下拉框适配
  91. /// </summary>
  92. public class ComBoxValueProvider : ElementValueProvider
  93. {
  94. public override object GetValue()
  95. {
  96. ComboBox cmb = RefControl as ComboBox;
  97. string str = string.Empty;
  98. if (cmb != null)
  99. {
  100. str = cmb.Text;
  101. }
  102. return str;
  103. }
  104. public override void SetValue(object value)
  105. {
  106. ComboBox cmb = RefControl as ComboBox;
  107. object str = value ?? string.Empty;
  108. if (cmb != null)
  109. {
  110. cmb.Text = str.ToString();
  111. if (cmb.SelectedIndex == -1 && cmb.Items.Count > 0 && !cmb.IsEditable)
  112. {
  113. cmb.SelectedIndex = 0;
  114. }
  115. }
  116. }
  117. protected override Type GetControlType()
  118. {
  119. return typeof(ComboBox);
  120. }
  121. }
  122. /// <summary>
  123. /// 单选框适配
  124. /// </summary>
  125. public class RadioValueProvider : ElementValueProvider
  126. {
  127. public override object GetValue()
  128. {
  129. RadioButton radioButton = RefControl as RadioButton;
  130. string str = string.Empty;
  131. if (radioButton != null)
  132. {
  133. str = radioButton.IsChecked.ToString();
  134. }
  135. return str;
  136. }
  137. public override void SetValue(object value)
  138. {
  139. RadioButton radioButton = RefControl as RadioButton;
  140. object str = value ?? string.Empty;
  141. if (radioButton != null)
  142. {
  143. bool isCheck =str.ToString().ToBoolean();
  144. if (isCheck) //如果是false则不给相应radio赋值,避免不必要的界面交互造成的bug
  145. radioButton.IsChecked = isCheck;
  146. }
  147. }
  148. protected override Type GetControlType()
  149. {
  150. return typeof(RadioButton);
  151. }
  152. }
  153. /// <summary>
  154. /// 多选框适配
  155. /// </summary>
  156. public class CheckBoxValueProvider : ElementValueProvider
  157. {
  158. public override object GetValue()
  159. {
  160. CheckBox checkBox = RefControl as CheckBox;
  161. string str = string.Empty;
  162. if (checkBox != null)
  163. {
  164. str = checkBox.IsChecked.ToString();
  165. }
  166. return str;
  167. }
  168. public override void SetValue(object value)
  169. {
  170. CheckBox checkBox = RefControl as CheckBox;
  171. object str = value ?? string.Empty;
  172. if (checkBox != null)
  173. {
  174. checkBox.IsChecked = str.ToString().ToBoolean();
  175. }
  176. }
  177. protected override Type GetControlType()
  178. {
  179. return typeof(CheckBox);
  180. }
  181. }
  182. /// <summary>
  183. /// 列表框框适配
  184. /// </summary>
  185. public class ListBoxValueProvider : ElementValueProvider
  186. {
  187. public override object GetValue()
  188. {
  189. ListBox listBox = RefControl as ListBox;
  190. string str ="0";
  191. if (listBox != null)
  192. {
  193. str = listBox.SelectedIndex.ToString();
  194. }
  195. return str;
  196. }
  197. public override void SetValue(object value)
  198. {
  199. ListBox listBox = RefControl as ListBox;
  200. object str = value ?? string.Empty;
  201. if (listBox != null)
  202. {
  203. int index = 0;
  204. if (int.TryParse(str.ToString(), out index))
  205. {
  206. if (listBox.Items.Count > index)
  207. {
  208. listBox.SelectedIndex = index;
  209. }
  210. }
  211. }
  212. }
  213. protected override Type GetControlType()
  214. {
  215. return typeof(ListBox);
  216. }
  217. }
  218. /// <summary>
  219. /// 文本框适配
  220. /// </summary>
  221. public class TextBlockValueProvider : ElementValueProvider
  222. {
  223. public override object GetValue()
  224. {
  225. TextBlock textBox = RefControl as TextBlock;
  226. string str = string.Empty;
  227. if (textBox != null)
  228. {
  229. str = textBox.Text;
  230. }
  231. return str;
  232. }
  233. public override void SetValue(object value)
  234. {
  235. TextBlock textBox = RefControl as TextBlock;
  236. object str = value ?? string.Empty;
  237. if (textBox != null)
  238. {
  239. textBox.Text = str.ToString();
  240. }
  241. }
  242. protected override Type GetControlType()
  243. {
  244. return typeof(TextBlock);
  245. }
  246. }
  247. /// <summary>
  248. /// 文本框适配
  249. /// </summary>
  250. public class WindowValueProvider : ElementValueProvider
  251. {
  252. public override object GetValue()
  253. {
  254. Window window = RefControl as Window;
  255. string str = string.Empty;
  256. if (window != null)
  257. {
  258. List<string> valueList = new List<string>();
  259. valueList.Add(window.Top.ToString());
  260. valueList.Add(window.Left.ToString());
  261. valueList.Add(window.Width.ToString());
  262. valueList.Add(window.Height.ToString());
  263. str = string.Join(",", valueList);
  264. }
  265. return str;
  266. }
  267. public override void SetValue(object value)
  268. {
  269. Window window = RefControl as Window;
  270. string str = value?.ToString()?? string.Empty;
  271. if (string.IsNullOrEmpty(str)|| window != null)
  272. {
  273. List<string> valueList = str.Split(',').ToList();
  274. try
  275. {
  276. if (valueList.Count > 3)
  277. {
  278. window.Top = valueList[0].ToDouble();
  279. window.Left = valueList[1].ToDouble();
  280. window.Width = valueList[2].ToDouble();
  281. window.Height = valueList[3].ToDouble();
  282. }
  283. }
  284. catch (Exception)
  285. {
  286. System.Diagnostics.Debug.WriteLine("窗口赋值出错");
  287. }
  288. }
  289. }
  290. protected override Type GetControlType()
  291. {
  292. return typeof(Window);
  293. }
  294. }
  295. }