Creating my WPF C# Treeview MVVM Model

Hi guys,

I found myself in need to create a WPF MVVM model that will do a few things:

  1. I can view it as a user inside a XAML TreeView, therefore it has to have some sort of hierarchy
  2. I can print it in XML files
  3. I can load it from XML files
  4. The model itself can have at least one observable collection of objects

Having that in mind I created the following base class:

 
 /// <summary>
 /// This is my base Tree View Class that inherits from the INotifyPropertyChanged interface
 /// It implements the isSelectedProperty and isExpandedProperty properties using the PropertyChangedEventHandler
 /// </summary>
 public class TreeViewItemBase : INotifyPropertyChanged
 {
 private bool isSelectedProperty;
 public bool IsSelected
 {
 get { return this.isSelectedProperty; }
 set
 {
 if (value != this.isSelectedProperty)
 {
 this.isSelectedProperty = value;
 NotifyPropertyChanged("IsSelected");
 }
 }
 }

 private bool isExpandedProperty;
 public bool IsExpanded
 {
 get { return this.isExpandedProperty; }
 set
 {
 if (value != this.isExpandedProperty)
 {
 this.isExpandedProperty = value;
 NotifyPropertyChanged("IsExpanded");
 }
 }
 }


 public event PropertyChangedEventHandler PropertyChanged;

 public void NotifyPropertyChanged(string propertyName)
 {
 if (this.PropertyChanged != null)
 this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 }
 }


The actual class I created to save the data that have the requirements specified above looks like:

 /// <summary>
 /// This tree model is for the Data profile
 /// </summary>
 public class TreeModel : TreeViewItemBase
 {
 public TreeModel()
 {
 this.Nodes = new ObservableCollection<TreeNode>();
 }

 public string Type { get; set; }
 public ObservableCollection<TreeNode> Nodes { get; set; }
 }


 public class TreeNode
 {
 public string controlType { get; set; }
 public string controlValue { get; set; }
 public string fullFileLocation { get; set; } // this property is used on the export and import functions only.
 }

 

Now I can use this TreeModel class to save all the data the user provides me in the XAML TabControls as they are sampled in my previous post WPF C# TabControl example

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *