Problems using SubObject with GoXmlBindingTransfor

I currently use GoDiagram v3 (GoDiagram for Net v3.0.3.3), i have problems using sub object with GoXmlBindingTransformer. All direct properties of the object is saved perfectly but properties of childrens of my main object have problems when i reload it, i can’t see why.

I made a small sample to illustrate my problem with your demo sample FlowCharter (GoDiagram Win 3.0.3 for .NET 3.5\Samples\FlowCharter). The result of my test, i can save "Size", "Position" easilly but sub object properties "drawingPart.BrushColor" cannot be saved (even if i add a properties to have a direct access to it), why and have very strange result?
To Reproduce my problems you need to add code at this 3 following places in FlowCharter sample:
1) Code to save my new object "ISwRootDrawingObj" in GraphDoc.cs
private static void InitReaderWriter(GoXmlReaderWriterBase rw) {
...
t = new GoXmlBindingTransformer("ISwRootDrawingObj", new ISwRootDrawingObj());
t.IdAttributeUsedForSharedObjects = true;
t.HandlesNamedPorts = true;
t.AddBinding("Size", "Size");
t.AddBinding("Location", "Location");
//t.AddBinding("ObjBrushColor", "drawingPart.BrushColor");
t.AddBinding("ObjBrushColor", "ObjBrushColor");
rw.AddTransformer(t);
...
}
2) Code to add my new object toolbox in MainForm.cs
private void InitializeCatalog() {
...
ISwRootDrawingObj testObj = new ISwRootDrawingObj(GoFigure.Ellipse, new SizeF(100, 100));
testObj.drawingPart.BrushColor = Color.Black;
myPalette.Document.Add(testObj);
...
}
3) Add my new objectclass file to the project (ISwRootDrawingObj .cs):
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Collections;
using System.Resources;
using System.Windows.Forms;
using Northwoods.Go;
namespace FlowCharter
{
[Serializable]
public class ISwRootDrawingObj : GoGroup
{
public ISwRootDrawingObj()
: base()
{
drawingPart = new GoDrawing(GoFigure.Ellipse);
drawingPart.Selectable = false;
Add(drawingPart);
}
public ISwRootDrawingObj(GoFigure f, SizeF size)
{
drawingPart = new GoDrawing(f);
drawingPart.Selectable = false;
Add(drawingPart);
this.Size = size;
}

#region FieldProperties
public GoDrawing drawingPart = null;

public Color ObjBrushColor
{
get
{
return drawingPart.BrushColor;
}
set
{
drawingPart.BrushColor = value;
}
}
#endregion FieldProperties
public override GoContextMenu GetContextMenu(GoView view)
{
if (view is GoOverview) return null;
if (!(view.Document is GraphDoc)) return null;
GoContextMenu cm = new GoContextMenu(view);
if (!((GraphDoc)view.Document).IsReadOnly)
{
cm.MenuItems.Add(new MenuItem("Change Color", new EventHandler(this.ChangeColor_Command)));
}
return cm;
}
public void ChangeColor_Command(Object sender, EventArgs e)
{
Random random = new Random();
int r = random.Next(0, 255);
int g = random.Next(0, 255);
int b = random.Next(0, 255);
ObjBrushColor = Color.FromArgb(r, g, b);
//drawingPart.BrushColor = Color.FromArgb(r, g, b);
float size = random.Next(10, 150);
Size = new SizeF(size, size);
}
}
}
PS: You need to use right click menu to edit size and color of the selected object.

There’s a couple of problems. First… the issue with the XML isn’t BrushColor, it’s the way drawingPart is defined.



there’s a Trace message that helps here:



No property ‘drawingPart’ on type: FlowCharter.ISwRootDrawingObj, for attribute ‘ObjBrushColor’



To be used in the XML support, it has to be a property.



public GoDrawing drawingPart {

get {

return this[0] as GoDrawing;

}

}



This code also shows how to fix the second problem, which is storage in your class without a CopyChildren method.



Code your constructors like this:



GoDrawing dp = new GoDrawing(GoFigure.Ellipse);

dp.Selectable = false;

Add(dp);



(this[0] is now the first element of the GoGroup… the GoDrawing object. and, there is no drawingPart to initialize now on a copy)



or, you can look at other sample classes to see how CopyChildren works.

Thanks for the quick reply, now i known how to use it!