I have modified the FolderBrowserModel, and I'm able to display nodes which I have written into my code whilst writing it. However I would like to be able to add nodes at runtime. I have a proberty in my display model which I can use to change the value of each node, and this works correctly. I have tried to use this property to add additional child nodes. The node is added to the list, but nothing appears in the tree view.
Is it actually possible to add additional nodes to the folderborwsermodel at runtime? I have pasted my source files below.
Andrew
MainForm.cs
/*
* Created by SharpDevelop.
* User: Andrew
* Date: 16/09/2015
* Time: 11:42
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Aga.Controls.Tree;
using Aga.Controls.Tree.NodeControls;
using Aga.Controls;
namespace TestApplication
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
private class ToolTipProvider : IToolTipProvider
{
public string GetToolTip(TreeNodeAdv node, NodeControl nodeControl)
{
return "Drag&Drop nodes to move them";
}
}
private TreeModel _model;
private Font _childFont;
private int count = 0;
MetaDisplayModel meta_model;
List <MetaItems> _metadata;
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
_name.ToolTipProvider = new ToolTipProvider();
_value.ToolTipProvider = new ToolTipProvider();
_model = new TreeModel();
_childFont = new Font(_tree.Font.FontFamily, 18, FontStyle.Bold);
meta_model = new MetaDisplayModel();
_tree.Model = new SortedTreeModel(meta_model);
timer1.Start();
}
void Timer1Tick(object sender, EventArgs e)
{
count++;
try
{
_metadata = meta_model._itemsDisplayed;
_metadata[0].Value = count * 10;
_metadata[1].Value = count;
_metadata.Add(new MetaData("Test", _metadata[0].Parent, _metadata[0].Owner));
_tree.Invalidate();
}
catch (Exception ex)
{
}
}
}
}
MetaDisplayModel.cs:
/*
* Created by SharpDevelop.
* User: Andrew
* Date: 29/09/2015
* Time: 11:20
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Aga.Controls.Tree;
using Aga.Controls.Tree.NodeControls;
using Aga.Controls;
namespace TestApplication
{
/// <summary>
/// Description of MetaDisplayModel.
/// </summary>
public class MetaDisplayModel: ITreeModel
{
public List<MetaItems> _itemsToDisplay;
private Dictionary<string,List<MetaItems>> _cache = new Dictionary<string,List<MetaItems>>();
public List<MetaItems> _itemsDisplayed
{
get
{
return _cache["Metadata set"];
}
set
{
_cache["Metadata set"] = _itemsDisplayed;
}
}
public MetaDisplayModel()
{
_itemsToDisplay = new List<MetaItems>();
}
private TreePath GetPath(MetaItems item)
{
if (item == null)
return TreePath.Empty;
else
{
Stack<object> stack = new Stack<object>();
while (item != null)
{
stack.Push(item);
item = item.Parent;
}
return new TreePath(stack.ToArray());
}
}
public System.Collections.IEnumerable GetChildren(TreePath treePath)
{
string[] meta_names = new string[7] {"Meta 1", "Meta 2", "Meta 3", "Meta 4", "Meta 5", "Meta 6", "Meta 7"};
List<MetaItems> items = null;
if (treePath.IsEmpty())
{
if (_cache.ContainsKey("ROOT"))
items = _cache["ROOT"];
else
{
items = new List<MetaItems>();
_cache.Add("ROOT", items);
items.Add(new RootItem("Metadata blank", this));
items.Add(new RootItem("Metadata set", this));
}
}
else
{
MetaItems parent = treePath.LastNode as MetaItems;
if (parent != null)
{
if (_cache.ContainsKey(parent.ItemName))
items = _cache[parent.ItemName];
else
{
if (parent.ItemName == "Metadata set")
{
items = new List<MetaItems>();
try
{
foreach (string str in meta_names)
{
items.Add(new MetaData(str, parent, this));
}
}
catch (IOException)
{
return null;
}
_cache.Add(parent.ItemName, items);
_itemsToDisplay.AddRange(items);
}
}
}
}
return items;
}
public bool IsLeaf(TreePath treePath)
{
return treePath.LastNode is MetaData;
}
public event EventHandler<TreeModelEventArgs> NodesChanged;
internal void OnNodesChanged(MetaItems item)
{
if (NodesChanged != null)
{
TreePath path = GetPath(item.Parent);
NodesChanged(this, new TreeModelEventArgs(path, new object[] { item }));
}
}
public event EventHandler<TreeModelEventArgs> NodesInserted;
public event EventHandler<TreeModelEventArgs> NodesRemoved;
public event EventHandler<TreePathEventArgs> StructureChanged;
public void OnStructureChanged()
{
if (StructureChanged != null)
StructureChanged(this, new TreePathEventArgs());
}
}
}
MetaItems.cs
/*
* Created by SharpDevelop.
* User: Andrew
* Date: 29/09/2015
* Time: 09:57
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using Aga.Controls.Tree;
using Aga.Controls.Tree.NodeControls;
using Aga.Controls;
namespace TestApplication
{
/// <summary>
/// Description of MetaItems.
/// </summary>
public abstract class MetaItems
{
string _name;
public string ItemName
{
get { return _name; }
set { _name = value; }
}
string _value;
public string ItemValue
{
get; //{ return _value; }
set;// { _value = value; }
}
private MetaItems _parent;
public MetaItems Parent
{
get { return _parent; }
set { _parent = value; }
}
public abstract string Name
{
get;
set;
}
public abstract int Value
{
get;
set;
}
private MetaDisplayModel _owner;
public MetaDisplayModel Owner
{
get { return _owner; }
set { _owner = value; }
}
}
public class RootItem : MetaItems
{
public RootItem(string name, MetaDisplayModel owner)
{
ItemName = name;
Owner = owner;
}
public override string Name
{
get
{
return ItemName;
}
set
{
}
}
public override int Value
{
get; //{ return _value; }
set;// { _value = value; }
}
}
public class MetaData : MetaItems
{
public MetaData(string name, MetaItems parent, MetaDisplayModel owner)
{
ItemName = name;
Parent = parent;
Owner = owner;
}
public override string Name
{
get
{
return ItemName;
}
set
{
}
}
public override int Value
{
get { return Convert.ToInt32(ItemValue); }
set { ItemValue = value.ToString(); }
}
}
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
TreeViewAdv introduction (link):
"If you wish TreeView to dynamically track model changes, you need to use one of several events of the ITreeModel interface. The most common is the StructureChanged event, which cause the TreeView to fully refresh the specified node (or empty, for the whole model)."
Hi
I have modified the FolderBrowserModel, and I'm able to display nodes which I have written into my code whilst writing it. However I would like to be able to add nodes at runtime. I have a proberty in my display model which I can use to change the value of each node, and this works correctly. I have tried to use this property to add additional child nodes. The node is added to the list, but nothing appears in the tree view.
Is it actually possible to add additional nodes to the folderborwsermodel at runtime? I have pasted my source files below.
Andrew
MainForm.cs
MetaDisplayModel.cs:
MetaItems.cs
TreeViewAdv introduction (link):
"If you wish TreeView to dynamically track model changes, you need to use one of several events of the ITreeModel interface. The most common is the StructureChanged event, which cause the TreeView to fully refresh the specified node (or empty, for the whole model)."
In MainForm.cs:
In MetaDisplayModel.cs:
(see also Aga.Contolds.Tree.TreeModelBase class)
Thankyou for getting back to me. I'm now able to add items as required.
When I add an item, the parent node collapses. I haven't worked out how to stop this, but I will post a follow up message when I do.
Do you use the last revision (rev 98) of TreeViewAdv ?
The rev 98 fixes issue #44 - tree nodes are now properly saved and restored when 'ITreeModel.StructureChanged' event is raised.
(use the "Code" tab and "Download Snapshot" button to get the last release)
I haven't got the latest revision, I will download it and give it a try.
Andrew
The latest revision does fix the issue. The parent node now stays open as I add items to the node.