Save and Load custom Controls

341 Views Asked by At

I'm using RadDiagram from Telerik

1- What I have: I applied same example in here http://www.telerik.com/help/wpf/raddiagram-extensions-toolbox.html But, the content of myShape is an image (I dnt have a geometry) when I drop it into RadDiagram it generates new instance of one of my custom classes in deserialization. I'm doing that by serializing the Header which is the hint key for me in deserializtion for generating the right custom class for the dropped shape.

My events:

void diagram_ShapeSerialized(object sender, SerializationEventArgs<IDiagramItem> e)
        {
            var shape = e.Entity as RadDiagramShape;
            if (shape != null)
            {
                var myShape = shape.DataContext as GalleryItem;                                              
                if (myShape != null)
                {
                        e.SerializationInfo["DataContent"] = myShape.Header;
                }
             }
}
    private void RadDiagram_ShapeDeserialized(object sender, ShapeSerializationRoutedEventArgs e)
    {
        var shape = e.Shape as RadDiagramShape;
        if (shape != null)
        {
            shape.Content = e.SerializationInfo["DataContent"].ToString();
            switch (shape.Content.ToString())
            {
                //Charts
                case "TimeChart":
                  shape.Content = new MyTimeChart(); break;

                case "ValueChart":
                  shape.Content = new MyTimeValue(); break;
               //...etc
            }
           }
         }

2- What I need: When I save the Diagram by owner.fileManager.SaveToFile(); I can't find inside the xml file the Content property or any SerializationInfo that's mean nothing could be serialized! I added this line

e.SerializationInfo["Content"] = myShape.Header;

in Serialization Event and still the same

I need to retrieve the same Design the user will make!

In Debug mode, Saving doesn't hit the Serialization Event - that means it depends on the first serialization! While it hits DeSerialization Event when I'm dropping the shape and when I'm loading the XML. And in Loading file time it always breaks inside DeSerialization Event at

            shape.Content = e.SerializationInfo["DataContent"].ToString();                     shape.Content = e.SerializationInfo["Content"].ToString();

and says "Object reference not set to an instance of an object"!

1

There are 1 best solutions below

0
On

I got the answer, serialize the item from dragging different than serialize it in saving
http://www.telerik.com/forums/save-and-load-custom-controls