我已將hashtable(ID and Name)
添加爲每個項目(節點)的自定義屬性。
用戶可以針對每個項目添加和更新這些屬性。
我的問題是,在更新特定項目的哈希表值時,例如在將節點的ID編輯爲2時將更新的值分配給所有項目(節點),然後每個節點包含相同的id值。
我認爲問題是該哈希表值沒有克隆該特定的項目,因爲像int vertexcount
其他屬性工作正常。添加並更新散列表(ID和名稱)作爲c#中每個項目(節點)的自定義屬性。
如何解決此問題以及如何更新該特定項目的屬性?在此先感謝..
這裏是一個性質的序列化代碼:
[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 Class 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
}
編輯屬性: grp
是一個節點:
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;
}
問題被提升,而複製被複制已經存在的節點並更新複製節點的屬性。
你將不得不向我們展示一些代碼,你如何實例化每個節點的散列表,或者你是否通過ref傳遞給所有節點? –
你在哪裏/你如何複製節點? –
我有複製nd通過設計師.....不在代碼中... 感謝您的回覆............ – Learner