123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using System;
- using System.Net;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Documents;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Animation;
- using System.Windows.Shapes;
- using VocabManager.Model;
- using VocabManager.DataLayer;
- using VocabManager.Command;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Linq;
- namespace VocabManager.ViewModel
- {
- public class ItemViewModel : ViewModelBase
- {
- public bool IsNew
- {
- get;
- set;
- }
- public Item Item
- {
- get;
- private set;
- }
- DeckViewModel _deckViewModel;
- public DeckViewModel DeckViewModel
- {
- get
- {
- return _deckViewModel;
- }
- }
- public ItemViewModel(Item item, DeckViewModel deckViewModel)
- {
- Item = item;
- _deckViewModel = deckViewModel;
- Item.DeckId = deckViewModel.Id;
-
- }
- public int DeckId
- {
- get
- {
- return Item.DeckId;
- }
- }
- protected override void OnPropertyChanged(string propertyName)
- {
- base.OnPropertyChanged(propertyName);
- }
- public int Index
- {
- get
- {
- return _deckViewModel.AllItems.IndexOf(this) + 1;
- }
- }
- public string FrontText
- {
- get
- {
- return Item.Front.Text ;
- }
- set
- {
- if (Item.Front.Text == value)
- return;
- Item.Front.Text = value;
- OnPropertyChanged("FrontText");
- }
- }
-
- public string BackText
- {
- get
- {
- return Item.Back.Text;
- }
- set
- {
- if (Item.Back.Text == value)
- return;
- Item.Back.Text = value;
- OnPropertyChanged("BackText");
- }
- }
- public string BackSound
- {
- get
- {
- return Item.Back.Sound;
- }
- set
- {
- if (Item.Back.Sound == value)
- return;
- Item.Back.Sound = value;
- OnPropertyChanged("BackSound");
- }
- }
- bool _isInEditMode;
- public bool IsInEditMode
- {
- get
- {
- return _isInEditMode;
- }
- set
- {
- if (_isInEditMode == value)
- return;
- _isInEditMode = value;
- OnPropertyChanged("IsInEditMode");
- }
- }
- bool _isFrontSide = true;
- public bool IsFrontSide
- {
- get
- {
- return _isFrontSide;
- }
- set
- {
- if (_isFrontSide == value)
- return;
- _isFrontSide = value;
- OnPropertyChanged( "IsFrontSide" );
- }
- }
- #region Edit
- RelayCommand _editCommand;
- public ICommand EditCommand
- {
- get
- {
- if (_editCommand == null)
- {
- _editCommand = new RelayCommand(
- param => this.Edit(),
- param => this.CanEdit()
- );
- }
- return _editCommand;
- }
- }
- private bool CanEdit()
- {
- return true;
- }
- private void Edit()
- {
- IsInEditMode = true;
- BeginEdit();
- }
- void BeginEdit()
- {
- _tempPropertyValues = new Dictionary<string, object>();
- _tempPropertyValues.Add("FrontText", this.FrontText);
- _tempPropertyValues.Add("BackText", this.BackText);
- }
- #endregion
-
- #region Cancel
- RelayCommand _cancelEditCommand;
- public ICommand CancelEditCommand
- {
- get
- {
- if (_cancelEditCommand == null)
- {
- _cancelEditCommand = new RelayCommand(
- param => this.CancelEdit(),
- param => this.CanCancelEdit()
- );
- }
- return _cancelEditCommand;
- }
- }
-
- private bool CanCancelEdit()
- {
- return IsInEditMode;
- }
- Dictionary<string, object> _tempPropertyValues;
- private void CancelEdit()
- {
- if (IsNew)
- _deckViewModel.RemoveCurrentCommand.Execute(null);
- else
- {
- this.FrontText = _tempPropertyValues["FrontText"] as string;
- this.BackText = _tempPropertyValues["BackText"] as string;
- }
- this.IsInEditMode = false;
- }
- #endregion
- #region Save
-
- RelayCommand _saveCommand;
- public ICommand SaveCommand
- {
- get
- {
- if (_saveCommand == null)
- {
- _saveCommand = new RelayCommand(
- param => this.Save(),
- param => this.CanSave()
- );
- }
- return _saveCommand;
- }
- }
- private void Save()
- {
- _deckViewModel.SaveItem(this);
- this.IsNew = this.IsInEditMode = false;
-
- }
-
- private bool CanSave()
- {
- return IsInEditMode;
- }
- #endregion
- #region Change Side
- internal void ChangeSide()
- {
- IsFrontSide = !IsFrontSide;
- }
- internal bool CanChangeSide()
- {
- return !IsInEditMode;
- }
- RelayCommand _changeSideCommand;
- public ICommand ChangeSideCommand
- {
- get
- {
- if (_changeSideCommand == null)
- {
- _changeSideCommand = new RelayCommand(
- param => this.ChangeSide(),
- param => this.CanChangeSide()
- );
- }
- return _changeSideCommand;
- }
- }
- #endregion
-
- }
- }
|