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?
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
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.
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
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
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,
Regards,
Deepa Thiruppathy
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();
}
...
}
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
You are welcome. Please let us know if you require any further assistance.