Can I use a collection in GraphModelNodeData

Hi,

I just want know whether I can use an ObservableCllection within GraphModelNodeData so I can share the structure for similar members of a node. Let's say I have a node which has 3 colors to be set in runtime, one for foreground, one for background and one for text color. I know that I may define 3 individual color properties there, but things are complicated for me, probably I will have some nodes with more properties like this, so it'd better that I can make a collection in GraphModelNodeData. I've tried to do like this:
public class MyData : GraphLinksModelNodeData
{
...
public clsColors Colors { get; set; }
}
public class clsColors : ObservableCollection
{
public clsColors(int num)
{
for (int i = 0; i < num; i++)
{
Add(new clsString() {Str="#FFFFFFE0"});
}
}
}
I run this with some problem, all nodes I've added into Diagram have the same color, it seems that all nodes point to the same memory pointer.
Could you please give me some suggestion or correct me with a right solution.
Thanks.

You certainly can have data properties that are observable collections. The DynamicPorts sample demonstrates this, although it does so by having four separate collections that specify the ports for the node on each of the four sides. (Each “Unit” corresponds to a node and has four collections of "Socket"s. Each “Socket” describes a port and even includes a color string.)

You didn’t show how your MyData.Colors property gets set. To avoid sharing collections you need to make sure it is set to a new ObservableCollection for each instance of MyData. And if the node data is copied, you need to make sure the field doesn’t share the reference to the collection.

I suggest you make sure the constructor allocates an ObservableCollection as the initial value of MyData.Colors. And be sure to implement an override of Clone to make a copy of the collection for the new MyData object.

Hi Walter,

Thanks for your reply and your solution is very valuable. I found that my problem came from Clone which I didn't make IClonable interface for my property, so all nodes be copied from the origin had duplicated color. Now I made an overide of Clone of the collection then the problem gone!
Thanks