This is a migrated thread and some comments may be shown as answers.

Problem with cusotm objects deserialization

1 Answer 110 Views
Diagram, DiagramRibbonBar, DiagramToolBox
This is a migrated thread and some comments may be shown as answers.
florian
Top achievements
Rank 1
florian asked on 03 May 2018, 12:57 PM

Hello, 

I have some problems when I try to deserialize custom objects which implements RadDiagramShape.

I have implemented a custom shape :

public class MyShape : ElementShape
{
    public override GraphicsPath CreatePath(System.Drawing.Rectangle bounds)
    {
        GraphicsPath path = new GraphicsPath();
        path.AddString("Custom", new System.Drawing.FontFamily("Arial"), 0, bounds.Width, Point.Empty, StringFormat.GenericTypographic);
        return path;
    }
}

And a custom diagramShape :

public sealed class MyDiagramShape : RadDiagramShape
    {
        public MyDiagramShape()
        {
            ShapeText = "";
            ElementShape = new MyShape();
            InternalElementColor = System.Drawing.Color.LightBlue;
        }
    }
 

I add this custom shape to my diagram:

MyDiagramShape diagramShape = new MyDiagramShape();
diagramShape.Position = new Telerik.Windows.Diagrams.Core.Point(100, 80);
radDiagram1.AddShape(diagramShape);
 

This works fine, and when I serialize, I get a XML like this: 

<RadDiagram>
 <Shapes>
  <MyDiagramShape Type="WindowsFormsApplicationCustomShape.MyDiagramShape"
 </Shapes>
</RadDiagram>

 

Problems come when I try to load this XML, object instanciated are typed as RadDiagramShape and not MyDiagramShape.

When I serialize again, the structure of the XML is like this :

<RadDiagram>
<Shapes>
  <RadDiagramShape Type="Telerik.WinControls.UI.RadDiagramShape"
</Shapes>
</RadDiagram>

 

I don't know if it's possible to instanciate objects with their derived type like specified in the XML ?

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
florian
Top achievements
Rank 1
answered on 04 May 2018, 08:42 AM

I have made a quick workaround.

I manually instanciate the desired shape on ShapeDeserialized event.

private void RadDiagram1OnShapeDeserialized(object sender, SerializationEventArgs<IShape> e)
       {
           string type = e.SerializationInfo["Type"].ToString();
           RadDiagramShape radDiagramShape = (RadDiagramShape)e.Entity;
            
           if (type == typeof(MyDiagramShape).ToString())
           {
               MyDiagramShape shape = new MyDiagramShape();
               shape.Shape = radDiagramShape.Shape;
               shape.Position = radDiagramShape.Position;
               radDiagram1.AddShape(shape);
           }
       }

 

And I remove the redundant RadDiagramShape item 

private void RadDiagram1OnItemsChanging(object sender, DiagramItemsChangingEventArgs e)
        {
            var addedItem = e.NewItems.FirstOrDefault();
            if (addedItem != null )
            {
                if (addedItem.GetType() == typeof(RadDiagramShape))
                {
                    e.Cancel = true;
                }
            }
        }

 

I'm not able to work both with custom and standard shapes and it's not very elegant but at least it does the job !

Tags
Diagram, DiagramRibbonBar, DiagramToolBox
Asked by
florian
Top achievements
Rank 1
Answers by
florian
Top achievements
Rank 1
Share this question
or