2011-09-06 50 views
1

我已將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; 
} 

問題被提升,而複製被複制已經存在的節點並更新複製節點的屬性。

+1

你將不得不向我們展示一些代碼,你如何實例化每個節點的散列表,或者你是否通過ref傳遞給所有節點? –

+0

你在哪裏/你如何複製節點? –

+0

我有複製nd通過設計師.....不在代碼中... 感謝您的回覆............ – Learner

回答

0

嘗試通過兩個對象,原始和「克隆」從

public DynamicGroup(DynamicGroup src): base(src) 
{ 
    this.Properties = src.Properties; 
} 

改變你的構造器

public DynamicGroup(DynamicGroup src): base(src) 
{ 
    this.Properties = (Hashtable)src.Properties.Clone(); 
} 

否則安全哈希表將被引用。

+0

感謝您的迴應,它的效果很好.......... – Learner

0

您傳遞了一個的引用這裏:

public DynamicGroup(DynamicGroup src) 
     : base(src) 
    { 
     this.Properties = src.Properties; 
    } 

我想你可以有2個選項:

  1. 不要在這裏傳遞一個參考,但必須建立一個Properties內部集合DynamicMember,如果你需要應用父節點的一些變化給孩子一個,做一個功能UpdateChildren迭代遍歷指定節點的每個孩子。

  2. 順便說一下,有一個成員本地Properties像在第一個選項,但接受一個Properties並將值複製到本地變種。

我從代碼中看到,你的字典中的Keys基本上是值類型。

+0

感謝您的努力......... ........ – Learner

相關問題