ItemView.xaml.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Documents;
  8. using System.Windows.Input;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Animation;
  11. using System.Windows.Shapes;
  12. using VocabManager.ViewModel;
  13. using Microsoft.Phone.Shell;
  14. namespace VocabManager.View
  15. {
  16. public partial class ItemView : UserControlBase
  17. {
  18. ItemViewModel _viewModel;
  19. public ItemView()
  20. {
  21. InitializeComponent();
  22. }
  23. public void SetViewModel(ItemViewModel viewModel)
  24. {
  25. _viewModel = viewModel;
  26. }
  27. protected override void OnLoad()
  28. {
  29. base.OnLoad();
  30. SetViewModel(this.DataContext as ItemViewModel);
  31. _viewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(_viewModel_PropertyChanged);
  32. InitChildBasedViewModelMode(); ;
  33. }
  34. void _viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
  35. {
  36. if (e.PropertyName == "IsInEditMode")
  37. {
  38. InitChildBasedViewModelMode();
  39. }
  40. }
  41. private void InitChildBasedViewModelMode()
  42. {
  43. if (_viewModel.IsInEditMode)
  44. LoadNewChild(new ItemViewModeEdit());
  45. else
  46. LoadNewChild(new ItemViewModeView());
  47. }
  48. private void LoadNewChild(UserControlBase newView)
  49. {
  50. this.LayoutRoot.Children.Clear();
  51. this.LayoutRoot.Children.Add(newView);
  52. }
  53. protected override void OnUnload()
  54. {
  55. base.OnUnload();
  56. _viewModel.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(_viewModel_PropertyChanged);
  57. }
  58. }
  59. }