ItemViewModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Net;
  3. using System.Windows;
  4. using System.Windows.Controls;
  5. using System.Windows.Documents;
  6. using System.Windows.Ink;
  7. using System.Windows.Input;
  8. using System.Windows.Media;
  9. using System.Windows.Media.Animation;
  10. using System.Windows.Shapes;
  11. using VocabManager.Model;
  12. using VocabManager.DataLayer;
  13. using VocabManager.Command;
  14. using System.Collections.Generic;
  15. using System.Reflection;
  16. using System.Linq;
  17. namespace VocabManager.ViewModel
  18. {
  19. public class ItemViewModel : ViewModelBase
  20. {
  21. public bool IsNew
  22. {
  23. get;
  24. set;
  25. }
  26. public Item Item
  27. {
  28. get;
  29. private set;
  30. }
  31. DeckViewModel _deckViewModel;
  32. public DeckViewModel DeckViewModel
  33. {
  34. get
  35. {
  36. return _deckViewModel;
  37. }
  38. }
  39. public ItemViewModel(Item item, DeckViewModel deckViewModel)
  40. {
  41. Item = item;
  42. _deckViewModel = deckViewModel;
  43. Item.DeckId = deckViewModel.Id;
  44. }
  45. public int DeckId
  46. {
  47. get
  48. {
  49. return Item.DeckId;
  50. }
  51. }
  52. protected override void OnPropertyChanged(string propertyName)
  53. {
  54. base.OnPropertyChanged(propertyName);
  55. }
  56. public int Index
  57. {
  58. get
  59. {
  60. return _deckViewModel.AllItems.IndexOf(this) + 1;
  61. }
  62. }
  63. public string FrontText
  64. {
  65. get
  66. {
  67. return Item.Front.Text ;
  68. }
  69. set
  70. {
  71. if (Item.Front.Text == value)
  72. return;
  73. Item.Front.Text = value;
  74. OnPropertyChanged("FrontText");
  75. }
  76. }
  77. public string BackText
  78. {
  79. get
  80. {
  81. return Item.Back.Text;
  82. }
  83. set
  84. {
  85. if (Item.Back.Text == value)
  86. return;
  87. Item.Back.Text = value;
  88. OnPropertyChanged("BackText");
  89. }
  90. }
  91. public string BackSound
  92. {
  93. get
  94. {
  95. return Item.Back.Sound;
  96. }
  97. set
  98. {
  99. if (Item.Back.Sound == value)
  100. return;
  101. Item.Back.Sound = value;
  102. OnPropertyChanged("BackSound");
  103. }
  104. }
  105. bool _isInEditMode;
  106. public bool IsInEditMode
  107. {
  108. get
  109. {
  110. return _isInEditMode;
  111. }
  112. set
  113. {
  114. if (_isInEditMode == value)
  115. return;
  116. _isInEditMode = value;
  117. OnPropertyChanged("IsInEditMode");
  118. }
  119. }
  120. bool _isFrontSide = true;
  121. public bool IsFrontSide
  122. {
  123. get
  124. {
  125. return _isFrontSide;
  126. }
  127. set
  128. {
  129. if (_isFrontSide == value)
  130. return;
  131. _isFrontSide = value;
  132. OnPropertyChanged( "IsFrontSide" );
  133. }
  134. }
  135. #region Edit
  136. RelayCommand _editCommand;
  137. public ICommand EditCommand
  138. {
  139. get
  140. {
  141. if (_editCommand == null)
  142. {
  143. _editCommand = new RelayCommand(
  144. param => this.Edit(),
  145. param => this.CanEdit()
  146. );
  147. }
  148. return _editCommand;
  149. }
  150. }
  151. private bool CanEdit()
  152. {
  153. return true;
  154. }
  155. private void Edit()
  156. {
  157. IsInEditMode = true;
  158. BeginEdit();
  159. }
  160. void BeginEdit()
  161. {
  162. _tempPropertyValues = new Dictionary<string, object>();
  163. _tempPropertyValues.Add("FrontText", this.FrontText);
  164. _tempPropertyValues.Add("BackText", this.BackText);
  165. }
  166. #endregion
  167. #region Cancel
  168. RelayCommand _cancelEditCommand;
  169. public ICommand CancelEditCommand
  170. {
  171. get
  172. {
  173. if (_cancelEditCommand == null)
  174. {
  175. _cancelEditCommand = new RelayCommand(
  176. param => this.CancelEdit(),
  177. param => this.CanCancelEdit()
  178. );
  179. }
  180. return _cancelEditCommand;
  181. }
  182. }
  183. private bool CanCancelEdit()
  184. {
  185. return IsInEditMode;
  186. }
  187. Dictionary<string, object> _tempPropertyValues;
  188. private void CancelEdit()
  189. {
  190. if (IsNew)
  191. _deckViewModel.RemoveCurrentCommand.Execute(null);
  192. else
  193. {
  194. this.FrontText = _tempPropertyValues["FrontText"] as string;
  195. this.BackText = _tempPropertyValues["BackText"] as string;
  196. }
  197. this.IsInEditMode = false;
  198. }
  199. #endregion
  200. #region Save
  201. RelayCommand _saveCommand;
  202. public ICommand SaveCommand
  203. {
  204. get
  205. {
  206. if (_saveCommand == null)
  207. {
  208. _saveCommand = new RelayCommand(
  209. param => this.Save(),
  210. param => this.CanSave()
  211. );
  212. }
  213. return _saveCommand;
  214. }
  215. }
  216. private void Save()
  217. {
  218. _deckViewModel.SaveItem(this);
  219. this.IsNew = this.IsInEditMode = false;
  220. }
  221. private bool CanSave()
  222. {
  223. return IsInEditMode;
  224. }
  225. #endregion
  226. #region Change Side
  227. internal void ChangeSide()
  228. {
  229. IsFrontSide = !IsFrontSide;
  230. }
  231. internal bool CanChangeSide()
  232. {
  233. return !IsInEditMode;
  234. }
  235. RelayCommand _changeSideCommand;
  236. public ICommand ChangeSideCommand
  237. {
  238. get
  239. {
  240. if (_changeSideCommand == null)
  241. {
  242. _changeSideCommand = new RelayCommand(
  243. param => this.ChangeSide(),
  244. param => this.CanChangeSide()
  245. );
  246. }
  247. return _changeSideCommand;
  248. }
  249. }
  250. #endregion
  251. }
  252. }