In questo modo potete usare il Binding su tutte le proprietà del TreeNode; in particolare a me interessava collegare la proprietà “Name” di un mio oggetto alla proprietà “Text” di un TreeNode.
Ecco un esempio in C# dell’implementazione del databindings per la proprietà “Text” di un oggetto TreeNode:
public class TreeNodeBind : TreeNode, IBindableComponent
{
//Implementazione dei costruttori base
public TreeNodeBind()
: base()
{
}
public TreeNodeBind(string text)
: base(text)
{
}
public TreeNodeBind(string text, TreeNode[] children)
: base(text, children)
{
}
public TreeNodeBind(string text, int imgIndex, int selImgIndex)
: base(text, imgIndex, selImgIndex)
{
}
public TreeNodeBind(string text, int imgIndex, int selImgIndex, TreeNode[] children)
: base(text, imgIndex, selImgIndex, children)
{
}
//Implementazione di IBindableComponent
#region IBindableComponent Members
private BindingContext _bindingContext;
private ControlBindingsCollection _dataBindings;
[Browsable(false)]
public BindingContext BindingContext
{
get
{
if (_bindingContext == null)
{
_bindingContext = new BindingContext();
}
return _bindingContext;
}
set
{
_bindingContext = value;
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ControlBindingsCollection DataBindings
{
get { return _dataBindings ?? (_dataBindings = new ControlBindingsCollection(this)); }
}
public ISite Site { get; set; }
public event EventHandler Disposed;
public void Dispose()
{
if (Disposed != null)
Disposed(this, EventArgs.Empty);
}
#endregion
}
Ora potete creare ed usare nodi di tipo TreeNodeBind che supportano il Binding:TreeNodeBind nodoBind = new TreeNodeBind();
nodoBind.DataBindings.Add(new System.Windows.Forms.Binding("Text", oggettoConDati, "Name", true));
tree.Nodes.Add(nodoBind);
Nessun commento:
Posta un commento