Adding and updating hashtable(ID and Name) as custom property for each items(Nodes) in c#
I have added hashtable(ID and Name)
as custom property for each item (Node).
int vertexcount
are working fine.
How to resolve this problem and how to update the property with respect to that particular item? Thanks in advance..
Here is the code for serialization of properties:
[Serializable]
public class DynamicGroup : Group
{
#region Class members
private int clickCount;
#endregion
#region Class Initialize/Finalize methods
/// <summary>
/// Default constructor.
/// </summary>
public DynamicGroup()
{
}
/// <summary>
/// Initialize a new instance of DynamicGroup.
/// </summary>
/// <param name="nodes">The node collection.</param>
public DynamicGroup(NodeCollection nodes)
: base(nodes)
{ }
/// <summary>
/// Copy constructor.
/// </summary>
/// <param name="src"></param>
public DynamicGroup(DynamicGroup src)
: base(src)
{
this.Properties = src.Properties;
}
/// <summary>
/// Serialization constructor for the MySymbol class.
/// </summary>
/// <param name="info">Serialization state information</param>
/// <param name="context">Streaming context information</param>
protected DynamicGroup(SerializationInfo info, StreamingContext context)
: base(info, context)
{
// The Serialization constructor is invoked during deserialization or during a drag & drop operation.
// If the MySymbol type has serializable members, then initialize them with the serialized data
// obtained from the SerializationInfo param
foreach (SerializationEntry entry in info)
{
switch (entry.Name)
{
case "properties":
properties = (Hashtable)entry.Value;
break;
case "clickCount":
clickCount = (int)entry.Value;
break;
}
}
}
#endregion
#region C开发者_运维知识库lass properties
/// <summary>
/// Get or set the dynamic properties collection.
/// </summary>
private Hashtable properties = null;
[Browsable(true), ReadOnly(false)]
public Hashtable Properties
{
get
{
if (properties == null)
{
properties = new Hashtable();
}
return properties;
}
set
{
properties = value;
}
}
[Browsable(true)]
public int ClickCount
{
get
{
return clickCount;
}
set
{
clickCount = value;
}
}
#endregion
[EventHandlerPriority(true)]
protected override void OnMouseClick(EventArgs e)
{
clickCount = clickCount + 1;
}
#region Class overrides
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>Copy of the object this method is invoked against</returns>
public override object Clone()
{
return new DynamicGroup(this);
}
/// <summary>
/// Populates a SerializationInfo with the data needed to serialize the target object.
/// </summary>
/// <param name="info">SerializationInfo object to populate.</param>
/// <param name="context">Destination streaming context.</param>
protected override void GetObjectData(SerializationInfo info, StreamingContext context)
{
base.GetObjectData(info, context);
//Add the serialization entry for dynamic properties collection
info.AddValue("properties", this.properties);
info.AddValue("clickCount", clickCount);
}
#endregion
}
Editing properties:
grp
is a node:
private void button2_Click(object sender, EventArgs e)
{
DynamicGroup grp = diagram1.Controller.SelectionList[0] as DynamicGroup;
listBox1.Items.Add(grp.ClickCount.ToString());
listBox1.Items.Add(grp.Properties["Name"].ToString());
textBox1.Text = grp.Properties["ID"].ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DynamicGroup grp = diagram1.Controller.SelectionList[0] as DynamicGroup;
grp.Properties["ID"] = textBox1.Text;
}
The issue is raised while duplicating that is copying the already existed node and update the property of duplicated node..
Try changing your Constructor from
public DynamicGroup(DynamicGroup src): base(src)
{
this.Properties = src.Properties;
}
to
public DynamicGroup(DynamicGroup src): base(src)
{
this.Properties = (Hashtable)src.Properties.Clone();
}
Otherwise the safe Hashtable will be referenced by both objects, the original and the "clone".
You pass a reference of a Dictionary
here :
public DynamicGroup(DynamicGroup src)
: base(src)
{
this.Properties = src.Properties;
}
I think you can have 2 options:
Do not pass a reference here but have to create a
Properties
collection inside DynamicMember and if you need apply some changes of parent node to child one, make a functionUpdateChildren
that iterates over every child of specified Node.By the way have a member local
Properties
like in first option, but accept in ctor aProperties
and copy values to local var.
I see from code that Keys
of your dictionary are basically value types.
精彩评论