Assuming you have a class for your tree nodes similar to:
[Serializable]
public class NodeBase : Base
{
NodeBase parent;
string name;
[Bindable(true)]
[Browsable(true)]
public NodeBase Parent
{
get
{
return parent;
}
set
{
if (parent == value)
return;
parent = value;
OnPropertyChanged("Parent");
}
}
public NodeBase Self
{
get
{
return this;
}
}
[Bindable(true)]
[Browsable(true)]
public string Name
{
get
{
return name;
}
set
{
if (name == value)
return;
name = value;
OnPropertyChanged("Name");
}
}
}
It would be nice to be able to implement a component exposing the list in a way suitable for binding on a form
public class NodeList : BindingComponent<NodeBase>
{
}
Here is a simple implmentation of BindingComponent. You can the use something like
BinaryFormatter[
^] to read and write the exposed BindingList.
public class BindingComponent<T> : Component,IBindingList, ITypedList
{
BindingList<T> items;
public event ListChangedEventHandler ListChanged;
public BindingComponent()
{
items = new BindingList<T>();
items.ListChanged += items_ListChanged;
}
void items_ListChanged(object sender, ListChangedEventArgs e)
{
if (ListChanged != null)
{
ListChanged(this, e);
}
}
protected override void Dispose(bool disposing)
{
items.ListChanged -= items_ListChanged;
base.Dispose(disposing);
}
[ListBindable(true)]
[Bindable(true)]
[Browsable(true)]
public BindingList<T> Items
{
get
{
return items;
}
}
public void AddIndex(PropertyDescriptor property)
{
((IBindingList)Items).AddIndex(property);
}
public object AddNew()
{
return Items.AddNew();
}
public bool AllowEdit
{
get
{
return Items.AllowEdit;
}
}
public bool AllowNew
{
get
{
return Items.AllowNew;
}
}
public bool AllowRemove
{
get
{
return Items.AllowRemove;
}
}
public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
{
((IBindingList)Items).ApplySort(property, direction);
}
public int Find(PropertyDescriptor property, object key)
{
return ((IBindingList)Items).Find(property, key);
}
public bool IsSorted
{
get
{
return ((IBindingList)Items).IsSorted;
}
}
public void RemoveIndex(PropertyDescriptor property)
{
((IBindingList)Items).RemoveIndex(property);
}
public void RemoveSort()
{
((IBindingList)Items).RemoveSort();
}
public ListSortDirection SortDirection
{
get
{
return ((IBindingList)Items).SortDirection;
}
}
public PropertyDescriptor SortProperty
{
get
{
return ((IBindingList)Items).SortProperty;
}
}
public bool SupportsChangeNotification
{
get
{
return ((IBindingList)Items).SupportsChangeNotification;
}
}
public bool SupportsSearching
{
get
{
return ((IBindingList)Items).SupportsSearching;
}
}
public bool SupportsSorting
{
get
{
return ((IBindingList)Items).SupportsSorting;
}
}
public int Add(object value)
{
return ((IBindingList)Items).Add(value);
}
public void Clear()
{
((IBindingList)Items).Clear();
}
public bool Contains(object value)
{
return ((IBindingList)Items).Contains(value);
}
public int IndexOf(object value)
{
return ((IBindingList)Items).IndexOf(value);
}
public void Insert(int index, object value)
{
((IBindingList)Items).Insert(index, value);
}
public bool IsFixedSize
{
get
{
return ((IBindingList)Items).IsFixedSize;
}
}
public bool IsReadOnly
{
get
{
return ((IBindingList)Items).IsReadOnly;
}
}
public void Remove(object value)
{
((IBindingList)Items).Remove(value);
}
public void RemoveAt(int index)
{
((IBindingList)Items).RemoveAt(index);
}
public object this[int index]
{
get
{
return Items[index];
}
set
{
Items[index] = (T)value;
}
}
public void CopyTo(Array array, int index)
{
((IBindingList)Items).CopyTo(array, index);
}
public int Count
{
get
{
return items.Count;
}
}
public bool IsSynchronized
{
get
{
return ((IBindingList)Items).IsSynchronized;
}
}
public object SyncRoot
{
get
{
return ((IBindingList)Items).SyncRoot;
}
}
public System.Collections.IEnumerator GetEnumerator()
{
return ((IBindingList)Items).GetEnumerator();
}
private PropertyDescriptorCollection propertyDescriptorCollection;
public PropertyDescriptorCollection GetItemProperties(PropertyDescriptor[] listAccessors)
{
if (propertyDescriptorCollection == null)
{
propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T), new Attribute[] { new BrowsableAttribute(true) });
}
return propertyDescriptorCollection;
}
public string GetListName(PropertyDescriptor[] listAccessors)
{
return typeof(T).Name;
}
}
Regards
Espen Harlinn