BehaviorNodeViewModel.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.ObjectModel;
  2. using System.ComponentModel;
  3. namespace Egametang
  4. {
  5. public class BehaviorNodeViewModel : INotifyPropertyChanged
  6. {
  7. private BehaviorNode node;
  8. private BehaviorNodeViewModel parent;
  9. private ObservableCollection<BehaviorNodeViewModel> children =
  10. new ObservableCollection<BehaviorNodeViewModel>();
  11. private bool isExpanded;
  12. private bool isSelected;
  13. public BehaviorNodeViewModel(BehaviorNode node, BehaviorNodeViewModel parent)
  14. {
  15. this.node = node;
  16. this.parent = parent;
  17. }
  18. public ObservableCollection<BehaviorNodeViewModel> Children
  19. {
  20. get
  21. {
  22. return children;
  23. }
  24. set
  25. {
  26. children = value;
  27. }
  28. }
  29. public string Name
  30. {
  31. get
  32. {
  33. return node.Name;
  34. }
  35. }
  36. public bool IsSelected
  37. {
  38. get
  39. {
  40. return isSelected;
  41. }
  42. set
  43. {
  44. if (value != isSelected)
  45. {
  46. isSelected = value;
  47. this.OnPropertyChanged("IsSelected");
  48. }
  49. }
  50. }
  51. public bool IsExpanded
  52. {
  53. get
  54. {
  55. return isExpanded;
  56. }
  57. set
  58. {
  59. if (value != isExpanded)
  60. {
  61. isExpanded = value;
  62. this.OnPropertyChanged("IsExpanded");
  63. }
  64. if (isExpanded && parent != null)
  65. {
  66. parent.IsExpanded = true;
  67. }
  68. this.LoadChildren();
  69. }
  70. }
  71. protected void LoadChildren()
  72. {
  73. foreach (var child in node.Children)
  74. {
  75. children.Add(new BehaviorNodeViewModel(child, this));
  76. }
  77. }
  78. public event PropertyChangedEventHandler PropertyChanged;
  79. protected virtual void OnPropertyChanged(string propertyName)
  80. {
  81. if (this.PropertyChanged != null)
  82. {
  83. this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
  84. }
  85. }
  86. }
  87. }