How to serialize the custom object in NodeViewModel?

Hi,

I try to add custom object to NodeViewModel and serialize this custom object with the node by node serialize function.


But I still can't find my custom object in serialize XML file.


My custom object:


 public class POS_Contract : INotifyPropertyChanged
    {

        public POS_Contract()
        {
            Nodettla = "ggla";
        }

        [DataMember]
        public string Nodettla
        {
            get;
            set;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

=======================

and my node add function:

  public void addPosNode(double OffsetX, double OffsetY)
        {

            sf_diagram.KnownTypes = () => new List<Type>()
                {
                    typeof(POS_Contract)
            };

            POS_Contract pc = new POS_Contract();

            NodeViewModel node = new NodeViewModel()
            {
                Content = pc,


                ID = "node_pos_" + Guid.NewGuid(),
                Constraints = NodeConstraints.Default & ~NodeConstraints.Resizable & ~NodeConstraints.Rotatable,
                OffsetX = OffsetX,
                OffsetY = OffsetY,
                Pivot = new Point(0, 0),
                UnitWidth = 120,
                UnitHeight = 40,
                Shape = App.Current.Resources["Process"],

                //Add node shape style
                ShapeStyle = stylePosNode.styleNode,

                Annotations = new ObservableCollection<AnnotationEditorViewModel>()
                {
                    new AnnotationEditorViewModel()
                    {
                        Content="POS",
                    }
                },
                Ports = new ObservableCollection<NodePortViewModel>()
                {
                    new NodePortViewModel()
                    {
                        ID = "POS_IN",
                        Constraints = PortConstraints.Default & ~PortConstraints.InheritPortVisibility,
                        PortVisibility = PortVisibility.Visible,
                        NodeOffsetX = 0.5 ,
                        NodeOffsetY = 0,
                        ShapeStyle = stylePosNode.styleInPort,
                        Shape = new EllipseGeometry() { RadiusX= 5, RadiusY = 5 },
                    },
                       new NodePortViewModel()
                    {
                           ID = "POS_OUT",
                        Constraints = PortConstraints.Default & ~PortConstraints.InheritPortVisibility,
                        PortVisibility = PortVisibility.Visible,
                        NodeOffsetX = 0.5 ,
                        NodeOffsetY = 1,
                        ShapeStyle = stylePosNode.styleInPort,
                        Shape = new EllipseGeometry() { RadiusX= 5, RadiusY = 5 },
                    }


                }
            };

            nodes.Add(node);

            sf_diagram.Nodes = nodes;

        }


======================

Thanks!

3 Replies

KR Karkuvel Rajan Shanmugavel Syncfusion Team April 3, 2018 10:32 AM UTC

Hi Nathaniel, 
 
NodeViewModel’s Content will not be serialized by default. Please add a custom property by deriving NodeViewModel and allow it to serialize as shown in following code example. 
 
Code Example: 
 
public class customnode : NodeViewModel 
{ 
    [DataMember] 
    public object customcontent 
    { 
        get { return Content; } 
        set { Content = value; } 
    } 
} 
 
 
 
We have prepared a sample to demonstrate this. 
 
 
Regards, 
Karkuvel Rajan.S 



NC Nathaniel Chen April 12, 2018 08:56 AM UTC

Thanks Rajan,

I understand and solved this problem. 

Appreciate your help :)


KR Karkuvel Rajan Shanmugavel Syncfusion Team April 13, 2018 06:01 AM UTC

Hi Nathaniel, 
 
Thanks, please let us know whether you need any other assistance. 
 
Regards, 
Karkuvel Rajan.S 


Loader.
Up arrow icon