Why we need to customize Tree view?
Intention is to modify default look and feel so that we can use the tree view as custom purpose control not just hierarchical data structure, a good example could be a folder explorer.
In this short article we are going to see how to customize tree view look and feel. first we will look at how to customize the style of a tree view item, then move on to the expand and collapse icon, and lastly we would see how to style a selected item.
Styling the items
Lets say we want to display a icon before each tree view “item node” so that the user knows what type of file item it is, right now we have drive, directory and file, so we would add thee resources to display. Bellow a simple code is given to demonstrate how to read a file system, and then populate in a dependency object so that it can be bind to a tree view.
[Sample c# code]
In the above code we have a enum FileType for distinguishing the file type. And a composite LiteDirectoryInfo where we have populated the directory structure. Finally After construction we preserved the data on FileSystem dependency property.
[Sample c# code for view c# part]
Above code shows only the part where I have assigned our custom presenter to the WPFFolderExplorer’s DataContext to follow MVP Pattern (Model,View,Presenter) where model is our own local drives and file system.
[Sample code for XAML is Given here]
In above xaml code its shown that how the binding with the presenter dependency is done. Also note that the item template is defined for a tree view.