# WPF Explorer TreeView with SelectedPath binding

DevFeed: [WPF Explorer TreeView with SelectedPath binding](<https://devfeed.tech/articles/wpf-explorer-treeview-with-selectedpath-binding-38404.md>)

Original publisher: [Read original article](<https://khmylov.com/2010/11/wpf-explorer-treeview-with-selectedpath-binding/>)

Author: Andrew Khmylov

Published: 2010-11-18T00:00:00Z

Content type: tutorial

Language: en

Sources: [Despite the odds](<https://devfeed.tech/sources/despite-the-odds.md>)

Topics: [WPF](<https://devfeed.tech/topics/wpf.md>), [Filesystems](<https://devfeed.tech/topics/filesystems.md>), [lazy loading](<https://devfeed.tech/topics/lazy-loading.md>)

Tags: [build](<https://devfeed.tech/tags/build.md>), [file](<https://devfeed.tech/tags/file.md>), [lazy-loading](<https://devfeed.tech/tags/lazy-loading.md>)

## AI overview

This tutorial describes a custom WPF TreeView control for browsing file systems and exposing a SelectedPath property for two-way binding. It uses lazy loading to populate drives and subfolders, and synchronizes the TreeView selection with the selected path.

## Source excerpt

While working on one of my projects, I had to implement a control for displaying file system. I found pretty good articles over the web ("A Simple WPF Explorer Tree" by Sacha Barber and some replies to it), but they were all about displaying folders. It was pretty easy to track currently selected folder, but there were some issues with its dynamic changes. So I decided to build my own control from scratch that will have some property like SelectedPath that will allow you to use TwoWay bindings to get and set selected folder at the tree. When user selects a node from the TreeView, text in the address bar is updated. When user enters address and clicks Update, TreeView selection gets updated. I have started with a simple CustomControl and inherited it from WPF TreeView: public class ExplorerTreeView: TreeView { public const string SelectedPathPropertyName = "SelectedPath"; public static readonly DependencyProperty SelectedPathProperty = DependencyProperty.Register( SelectedPathPropertyName, typeof (String), typeof (ExplorerTreeView)); public String SelectedPath { get { return (String) GetValue(SelectedPathProperty); } set { SetValue(SelectedPathProperty, value); } } } We will use some lazy-loading techniques to fill our TreeView with data. When control is loaded, it will display only drives on the user machine. Let's write a method for creating initial drive nodes: public void InitExplorer() { Items.Clear(); foreach (var drive in DriveInfo.GetDrives()) { Items.Add(GenerateDriveNode(drive)); } } private static TreeViewItem GenerateDriveNode(DriveInfo drive) { var item = new TreeViewItem { Tag = drive, Header = drive.ToString() }; item.Items.Add("*"); return item; } GenerateDriveNode method takes DriveInfo argument and builds TreeViewItem for it. Note that we are adding a dummy child node (the one with 'the star' header text). If we don't do this, then the user will not see a small button for expanding/collapsing for generated tree view item (it may look like '+' charac