How to get node's Annotation ?

I tried to use " Node.Annotations.ToString(); " to get the annotations of the node,but the return is indeed "

Syncfusion.UI.Xaml.Diagram.AnnotationCollection ".

How can I get the node's annotation correctly?


9 Replies

DT Deepa Thiruppathy Syncfusion Team June 28, 2022 07:40 AM UTC

Hi Tealer,


Requirement: How to get the annotation of a Node.


Annotations property of a node is a collection. You can get them by iterate one by one.


Code snippet:


foreach(IAnnotation annotation in node.Annotations as IEnumerable<object>)

{

   //You can do you own stuff here. For example getting label of the Annotation.

   string label = annotation.Content.ToString();

}


Regards,

Deepa Thiruppathy



TE tealer replied to Deepa Thiruppathy June 29, 2022 06:36 AM UTC

Thank you for your reply.

When I run this code, my project is gaving me an error :

     System.NullReferenceException: "Object reference not set to an instance of an object."

     Syncfusion.UI.Xaml.Diagram.Controls.IAnnotation.Content.get returns null.

But I'm sure I set the annotation to node.



DT Deepa Thiruppathy Syncfusion Team June 30, 2022 11:52 AM UTC

Hi Telaer,


Reported issue: How to get the annotation of a Node


We have created a simple sample. In that, we tried to get the annotation of a node in ItemSelectedEvent and we are not getting any null reference exception.


Sample link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/AnnotationSample1337424820



TE tealer July 1, 2022 12:51 AM UTC

I can't find problems in my project, it still reports same errors.

This is my project :

https://drive.google.com/file/d/1LQc7eQAVbzKsMHdPWtm35yztu1JU2zAG/view?usp=sharing



DT Deepa Thiruppathy Syncfusion Team July 1, 2022 09:49 AM UTC

Hi Tealer,


We have validated your project and applied the code snippet provided in our previous updates. We can get the node’s annotation without any error on ItemAdded event. We have modified your project and we have prepared a video representation to the scenario.


Sample and video link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SampleAndVideo-1604677260


If you are still facing the issue, please share below details with us,


  1. Product version
  2. Replication steps to reproduce the issue in your shared project
  3. Stack trace of the issue


Regards,

Deepa Thiruppathy



TE tealer replied to Deepa Thiruppathy July 4, 2022 02:47 AM UTC

Thank you for your replay.

But my problem is in the project path: DataBase/SQLiteOP.cs

in code :

public void InsertBasicNodesTableIteam(INode Node, IEnumerable<object> in_conn, IEnumerable<object> out_conn)

{

        ...

        // Get node annotation

        string node_annotaion = "";

        foreach(IAnnotation annotation in Node.Annotations as IEnumerable<object>)

        {

              // this line will cause an error that "Object reference not set to an instance of an object"

              node_annotaion = annotation.Content.ToString();

         }

       ...

}



DT Deepa Thiruppathy Syncfusion Team July 4, 2022 11:22 AM UTC

Hi Tealer,


Requirement: How to get the annotation of a Node


We have validated the mentioned file. In your application, at design time itself you have added a node called “leftPowerRail”. You have missed to add annotations to it. That’s why you have faced null object reference when you try to get annotation content by iterating Annotation collection.


Codes snippet:

CustomNode leftPowerRail = new CustomNode()

{

    //Constraints = NodeConstraints.Default & ~(NodeConstraints.Inherit | NodeConstraints.Draggable),

    Constraints = NodeConstraints.Default & ~(NodeConstraints.InheritDraggable), // node

    UnitWidth = 10,

    UnitHeight = lane.UnitHeight - 30, // 一定要设置的比Lane的初始unitheight小!!!

    LaneOffsetX = 40,

    LaneOffsetY = 10,

    Shape = App.Current.Resources["Rectangle"],

    Name = "LeftPowerRail",

    ID = "LeftPowerRail",

    Key = "LPR",

    Annotations = new ObservableCollection<IAnnotation>()

    {

         new AnnotationEditorViewModel()

         {

              Content = "LeftPowerRail",

         }

    },

    Ports = new PortCollection()

    {

         // dockport创建一整条的dock

         new DockPortViewModel()

         {

             SourcePoint = new Point(0, 1),

             TargetPoint = new Point(1, 1)

         }

    }

};

(this.Nodes as NodeCollection).Add(leftPowerRail);

 


Modified sample: https://www.syncfusion.com/downloads/support/directtrac/general/ze/SyncfusionWpfTest11679207129




TE tealer replied to Deepa Thiruppathy July 6, 2022 01:03 AM UTC

Thank you for solving my problem !



DT Deepa Thiruppathy Syncfusion Team July 6, 2022 06:33 AM UTC

You are welcome. Please let us know if you require any further assistance.


Loader.
Up arrow icon