mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Added basic use cases and new article
This commit is contained in:
@@ -17,12 +17,6 @@ namespace Terminal.Gui {
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
IList<ITreeNode> Children {get;}
|
||||
|
||||
/// <summary>
|
||||
/// The textual representation to be rendered when your class is visible in the tree
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
string Text {get;}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
139
UICatalog/Scenarios/TreeUseCases.cs
Normal file
139
UICatalog/Scenarios/TreeUseCases.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using Terminal.Gui;
|
||||
|
||||
namespace UICatalog.Scenarios {
|
||||
[ScenarioMetadata (Name: "Tree View", Description: "Simple tree view examples")]
|
||||
[ScenarioCategory ("Controls")]
|
||||
class TreeUseCases : Scenario {
|
||||
|
||||
View currentTree;
|
||||
|
||||
public override void Setup ()
|
||||
{
|
||||
Win.Title = this.GetName();
|
||||
Win.Y = 1; // menu
|
||||
Win.Height = Dim.Fill (1); // status bar
|
||||
Top.LayoutSubviews ();
|
||||
|
||||
var menu = new MenuBar (new MenuBarItem [] {
|
||||
new MenuBarItem ("_File", new MenuItem [] {
|
||||
new MenuItem ("_Quit", "", () => Quit()),
|
||||
}),
|
||||
new MenuBarItem ("_Scenarios", new MenuItem [] {
|
||||
new MenuItem ("_Simple Nodes", "", () => LoadSimpleNodes()),
|
||||
new MenuItem ("_Rooms", "", () => LoadRooms()),
|
||||
}),
|
||||
});
|
||||
|
||||
Top.Add (menu);
|
||||
|
||||
var statusBar = new StatusBar (new StatusItem [] {
|
||||
new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
|
||||
});
|
||||
|
||||
Top.Add (statusBar);
|
||||
|
||||
// Start with the most basic use case
|
||||
LoadSimpleNodes();
|
||||
}
|
||||
|
||||
// Your data class
|
||||
private class House : ITreeNode {
|
||||
|
||||
|
||||
// Your properties
|
||||
public string Address {get;set;}
|
||||
public List<Room> Rooms {get;set;}
|
||||
|
||||
// ITreeNode member:
|
||||
|
||||
public IList<ITreeNode> Children => Rooms.Cast<ITreeNode>().ToList();
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return Address;
|
||||
}
|
||||
}
|
||||
private class Room : ITreeNode{
|
||||
|
||||
public string Name {get;set;}
|
||||
|
||||
|
||||
// Rooms have no sub objects
|
||||
public IList<ITreeNode> Children => new List<ITreeNode>();
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
private void LoadRooms()
|
||||
{
|
||||
var myHouse = new House()
|
||||
{
|
||||
Address = "23 Nowhere Street",
|
||||
Rooms = new List<Room>{
|
||||
new Room(){Name = "Ballroom"},
|
||||
new Room(){Name = "Bedroom 1"},
|
||||
new Room(){Name = "Bedroom 2"}
|
||||
}
|
||||
};
|
||||
|
||||
if(currentTree != null)
|
||||
Win.Remove(currentTree);
|
||||
|
||||
var tree = new TreeView()
|
||||
{
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Width = 40,
|
||||
Height = 20
|
||||
};
|
||||
|
||||
Win.Add(tree);
|
||||
|
||||
tree.AddObject(myHouse);
|
||||
|
||||
currentTree = tree;
|
||||
}
|
||||
|
||||
private void Quit ()
|
||||
{
|
||||
Application.RequestStop ();
|
||||
}
|
||||
|
||||
private void LoadSimpleNodes()
|
||||
{
|
||||
if(currentTree != null)
|
||||
Win.Remove(currentTree);
|
||||
|
||||
var tree = new TreeView()
|
||||
{
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Width = 40,
|
||||
Height = 20
|
||||
};
|
||||
|
||||
Win.Add(tree);
|
||||
|
||||
var root1 = new TreeNode("Root1");
|
||||
root1.Children.Add(new TreeNode("Child1.1"));
|
||||
root1.Children.Add(new TreeNode("Child1.2"));
|
||||
|
||||
var root2 = new TreeNode("Root2");
|
||||
root2.Children.Add(new TreeNode("Child2.1"));
|
||||
root2.Children.Add(new TreeNode("Child2.2"));
|
||||
|
||||
tree.AddObject(root1);
|
||||
tree.AddObject(root2);
|
||||
|
||||
currentTree = tree;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,10 +7,6 @@ using Terminal.Gui;
|
||||
namespace UICatalog.Scenarios {
|
||||
[ScenarioMetadata (Name: "TreeViewFileSystem", Description: "Hierarchical file system explorer based on TreeView")]
|
||||
[ScenarioCategory ("Controls")]
|
||||
[ScenarioCategory ("Dialogs")]
|
||||
[ScenarioCategory ("Text")]
|
||||
[ScenarioCategory ("Dialogs")]
|
||||
[ScenarioCategory ("TopLevel")]
|
||||
class TreeViewFileSystem : Scenario {
|
||||
|
||||
/// <summary>
|
||||
|
||||
105
docfx/articles/treeview.md
Normal file
105
docfx/articles/treeview.md
Normal file
@@ -0,0 +1,105 @@
|
||||
# Tree View
|
||||
|
||||
TreeView is a control for navigating hierarchical objects. It comes in two forms `TreeView` and `TreeView<T>`.
|
||||
|
||||
## TreeView
|
||||
|
||||
The basic non generic TreeView class is populated by `ITreeNode` objects. The simplest tree you can make would look something like:
|
||||
|
||||
|
||||
```csharp
|
||||
var tree = new TreeView()
|
||||
{
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Width = 40,
|
||||
Height = 20
|
||||
};
|
||||
|
||||
Win.Add(tree);
|
||||
|
||||
var root1 = new TreeNode("Root1");
|
||||
root1.Children.Add(new TreeNode("Child1.1"));
|
||||
root1.Children.Add(new TreeNode("Child1.2"));
|
||||
|
||||
var root2 = new TreeNode("Root2");
|
||||
root2.Children.Add(new TreeNode("Child2.1"));
|
||||
root2.Children.Add(new TreeNode("Child2.2"));
|
||||
|
||||
tree.AddObject(root1);
|
||||
tree.AddObject(root2);
|
||||
|
||||
```
|
||||
|
||||
Having to create a bunch of TreeNode objects can be a pain especially if you already have your own objects e.g. `House`, `Room` etc. There are two ways to use your own classes without having to create nodes manually. Firstly you can implement the `ITreeNode` interface:
|
||||
|
||||
|
||||
```
|
||||
// Your data class
|
||||
private class House : ITreeNode {
|
||||
|
||||
|
||||
// Your properties
|
||||
public string Address {get;set;}
|
||||
public List<Room> Rooms {get;set;}
|
||||
|
||||
// ITreeNode member:
|
||||
|
||||
public IList<ITreeNode> Children => Rooms.Cast<ITreeNode>().ToList();
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return Address;
|
||||
}
|
||||
}
|
||||
|
||||
// Your other data class
|
||||
private class Room : ITreeNode{
|
||||
|
||||
public string Name {get;set;}
|
||||
|
||||
|
||||
// Rooms have no sub objects
|
||||
public IList<ITreeNode> Children => new List<ITreeNode>();
|
||||
|
||||
public override string ToString ()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
...
|
||||
|
||||
// After implementing the interface you can add your objects directly to the tree
|
||||
|
||||
var myHouse = new House()
|
||||
{
|
||||
Address = "23 Nowhere Street",
|
||||
Rooms = new List<Room>{
|
||||
new Room(){Name = "Ballroom"},
|
||||
new Room(){Name = "Bedroom 1"},
|
||||
new Room(){Name = "Bedroom 2"}
|
||||
}
|
||||
};
|
||||
|
||||
var tree = new TreeView()
|
||||
{
|
||||
X = 0,
|
||||
Y = 0,
|
||||
Width = 40,
|
||||
Height = 20
|
||||
};
|
||||
|
||||
tree.AddObject(myHouse);
|
||||
|
||||
|
||||
```
|
||||
|
||||
Alternatively you can simply tell the tree how the objects relate to one another by implementing `ITreeBuilder`. This is a good option if you don't have control of the data objects you are working with:
|
||||
|
||||
```
|
||||
TODO
|
||||
```
|
||||
|
||||
## TreeView<T>
|
||||
Reference in New Issue
Block a user