Two enhancements I can think of would be to display appropriate images for different file types and to provide a context menu so, for example, the user can open or delete a file. In this example, that's a little picture of a document. If the file_img parameter is at least 0, the method sets the new nodes' ImageIndex properties so they display the file image. This makes the directory's subdirectories come before the directory's files.įinally, the method loops through the directory's files, adding nodes below the directory's node. Next, the method loops through the directory's subdirectories, recursively calling AddDirectoryNodes for each. In this example, folders display a little folder image. If the folder_img parameter is at least 0, then the method sets the node's ImageIndex property to that value so the node displays an image. (This allows a program to add multiple top-level directories to the TreeView if desired.) Otherwise it adds the new node below the parent node. If the parent parameter is null, it adds the node to the TreeView control's Nodes collection. This method creates a TreeView node to represent the directory. If (file_img >= 0) file_node.ImageIndex = file_img TreeNode file_node = dir_(file_info.Name) If (folder_img >= 0) dir_node.ImageIndex = folder_img įoreach (DirectoryInfo subdir in dir_info.GetDirectories())ĪddDirectoryNodes(trv, subdir, dir_node, folder_img, file_img) įoreach (FileInfo file_info in dir_info.GetFiles()) If (parent = null) dir_node = (dir_info.Name) Įlse dir_node = (dir_info.Name) Public static void AddDirectoryNodes(TreeView trv,ĭirectoryInfo dir_info, TreeNode parent, int folder_img, int file_img) Add this directory's node and sub-nodes. It then calls the following AddDirectoryNode method. This method creates a DirectoryInfo object to represent the directory that should be loaded into the TreeView. String directory, int folder_img, int file_img)ĭirectoryInfo dir_info = new DirectoryInfo(directory) ĪddDirectoryNodes(trv, dir_info, null, folder_img, file_img) Public static void LoadFromDirectory(this TreeView trv, Initialize the TreeView from a directory, This example adds the following LoadFromDirectory extension method to the TreeView control. Title: Initialize a TreeView control to display a directory hierarchy in C# The following example shows how to create a TreeView where the TreeViewItem content contains DockPanel controls that have embedded content.C# Helper: Initialize a TreeView control to display a directory hierarchy in C# The following example shows how to create a TreeView where an XmlDataProvider provides TreeViewItem content and a HierarchicalDataTemplate defines the appearance of the content. ![]() The following example show how to define child elements of a TreeViewItem by defining Items that are Button controls. The following example shows how to explicitly define TreeViewItem content by setting the Header property to a text string. A TreeViewItem can also have TreeViewItem controls as its child elements and you can define these child elements by using the Items property. ![]() The Header property of the TreeViewItem contains the content that the TreeView displays for that item. This topic provides examples of these concepts. You can explicitly define the TreeView content or a data source can provide the content. This example shows how to create simple or complex TreeView controls.Ī TreeView consists of a hierarchy of TreeViewItem controls, which can contain simple text strings and also more complex content, such as Button controls or a StackPanel with embedded content.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |