Imports System.Collections.ObjectModel
Imports System.ComponentModel

' Classe che definisce un elemento di base del TreeView
Public Class TreeViewItemBase
   Implements INotifyPropertyChanged

   Friend m_Name As String
   Public Overridable Property Name As String
      Get
         Return m_Name
      End Get
      Set(value As String)
         If (value <> m_Name) Then
            m_Name = value
            NotifyPropertyChanged("Name")
         End If
      End Set
   End Property

   Friend m_isSelected As Boolean
   Public Overridable Property IsSelected As Boolean
      Get
         Return m_isSelected
      End Get
      Set(value As Boolean)
         If (value <> m_isSelected) Then
            m_isSelected = value
            NotifyPropertyChanged("IsSelected")
         End If
      End Set
   End Property

   Private m_isExpanded As Boolean
   Public Property IsExpanded As Boolean
      Get
         Return m_isExpanded
      End Get
      Set(value As Boolean)
         If (value <> m_isExpanded) Then
            m_isExpanded = value
            NotifyPropertyChanged("IsExpanded")
         End If
      End Set
   End Property

   Private m_IsActive As Boolean
   Public Property IsActive As Boolean
      Get
         Return m_IsActive
      End Get
      Set(value As Boolean)
         If value <> m_IsActive Then
            m_IsActive = value
            NotifyPropertyChanged("IsActive")
         End If
      End Set
   End Property

   Private m_IsEnabled As Boolean = True
   Public Property IsEnabled As Boolean
      Get
         Return m_IsEnabled
      End Get
      Set(value As Boolean)
         m_IsEnabled = value
         NotifyPropertyChanged("IsEnabled")
      End Set
   End Property

   Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

   Public Sub NotifyPropertyChanged(propName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
   End Sub

   Sub New(Name As String)
      m_Name = Name
   End Sub

   Sub New(Name As String, IsSelected As Boolean, IsExpanded As Boolean, IsActive As Boolean)
      Me.Name = Name
      Me.IsSelected = IsSelected
      Me.IsExpanded = IsExpanded
      Me.IsActive = IsActive
   End Sub

End Class

' Elemento del treeview che ha sottoelementi del suo stesso tipo
Public Class InheritableTreeViewItem
   Implements INotifyPropertyChanged

   Friend m_Name As String
   Public Property Name As String
      Get
         Return m_Name
      End Get
      Set(value As String)
         If (value <> m_Name) Then
            m_Name = value
            NotifyPropertyChanged("Name")
         End If
      End Set
   End Property

   Friend m_IsSelected As Boolean
   Public Overridable Property IsSelected As Boolean
      Get
         Return m_IsSelected
      End Get
      Set(value As Boolean)
         If (value <> m_IsSelected) Then
            m_IsSelected = value
            NotifyPropertyChanged("IsSelected")
         End If
      End Set
   End Property

   Private m_isExpanded As Boolean
   Public Property IsExpanded As Boolean
      Get
         Return m_isExpanded
      End Get
      Set(value As Boolean)
         If (value <> m_isExpanded) Then
            m_isExpanded = value
            NotifyPropertyChanged("IsExpanded")
         End If
      End Set
   End Property

   Private m_IsActive As Boolean
   Public Property IsActive As Boolean
      Get
         Return m_IsActive
      End Get
      Set(value As Boolean)
         If value <> m_IsActive Then
            m_IsActive = value
            NotifyPropertyChanged("IsActive")
         End If
      End Set
   End Property

   Private m_IsEnabled As Boolean = True
   Public Property IsEnabled As Boolean
      Get
         Return m_IsEnabled
      End Get
      Set(value As Boolean)
         m_IsEnabled = value
         NotifyPropertyChanged("IsEnabled")
      End Set
   End Property

   Private m_sPictureString As String
   Public Property PictureString As String
      Get
         Return m_sPictureString
      End Get
      Set(value As String)
         If value <> m_sPictureString Then
            m_sPictureString = value
            NotifyPropertyChanged("PictureString")
         End If
      End Set
   End Property

   Private m_Items As ObservableCollection(Of InheritableTreeViewItem)
   Public Property Items As ObservableCollection(Of InheritableTreeViewItem)
      Get
         Return m_Items
      End Get
      Set(value As ObservableCollection(Of InheritableTreeViewItem))
         m_Items = value
      End Set
   End Property

   Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

   Public Sub NotifyPropertyChanged(propName As String)
      RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propName))
   End Sub

   Sub New(Name As String)
      Me.Name = Name
      Me.Items = New ObservableCollection(Of InheritableTreeViewItem)
   End Sub

End Class

' Elemento che ha sottoelementi di tipo childitem
Public Class ParentItem
   Inherits TreeViewItemBase

   Private m_sPictureString As String
   Private m_Items As ObservableCollection(Of ChildItem)

   Public Property PictureString As String
      Get
         Return m_sPictureString
      End Get
      Set(value As String)
         If value <> m_sPictureString Then
            m_sPictureString = value
            NotifyPropertyChanged("PictureString")
         End If
      End Set
   End Property

   Public Property Items As ObservableCollection(Of ChildItem)
      Get
         Return m_Items
      End Get
      Set(value As ObservableCollection(Of ChildItem))
         m_Items = value
      End Set
   End Property

   Sub New(Name As String)
      MyBase.New(Name)
      Me.Items = New ObservableCollection(Of ChildItem)
   End Sub

   Sub New(Name As String, IsSelected As Boolean, IsExpanded As Boolean, IsActive As Boolean)
      MyBase.New(Name, IsSelected, IsExpanded, IsActive)
      Me.Items = New ObservableCollection(Of ChildItem)
   End Sub

End Class

' Elemento presente solo come figlio di un ParentItem
Public Class ChildItem
   Inherits TreeViewItemBase

   Private m_sPictureString As String
   Public Property PictureString As String
      Get
         Return m_sPictureString
      End Get
      Set(value As String)
         If value <> m_sPictureString Then
            m_sPictureString = value
            NotifyPropertyChanged("PictureString")
         End If
      End Set
   End Property

   Sub New(Name As String)
      MyBase.New(Name)
   End Sub

   Sub New(Name As String, IsSelected As Boolean, IsExpanded As Boolean, IsActive As Boolean)
      MyBase.New(Name, IsSelected, IsExpanded, IsActive)
   End Sub

End Class
