Referring to http://es.infragistics.com/samples/wpf/diagram/binding-nodes-with-objects
Can you please suggest recommended way of aligning new nodes if a child node is created on fly. I have set TreeDiagramLayout's NodeFlowDirection to "Below" but when a new object is added in viewmodel at run time it goes to top left corner of diagram.
Ideally starting from parent, I would like nodes to be center aligned and newly created to be below parent node.
Hello Abs, I've modified your sample accordingly to include the Item's CollectionChanged event. Keep in mind that by default it's expected that the Parent node will shift position to recenter it's place among it's children. So even when a child node is added in the right place the parent will shift left or right depending on where the children are added.
Let me know if you have any additional questions.
I still get the same exception, can you modify code attached in earlier post please?
Hello Abs,
This is a timing issue, since this event is firing well before nodes are displayed on-screen.
You can easily work around this by placing an if-check to determine if you have any shapes on-screen based on what is found in the datasource, otherwise you won't need to refresh the layout.
eg.
private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { FamilyTreeViewModel vm = this.Diagram.DataContext as FamilyTreeViewModel;
if (e.NewStartingIndex >= vm.FamilyTree.Count) { this.Diagram.RefreshLayout(); } }
Let me know if you have any questions regarding this matter.
Thanks Michael for the code sample to restrict vertical movement.
I am getting KeyNotFoundException with "The given key was not present in the dictionary" message when RefreshLayout is called on diagram's items CollectionChanged event.
The XamDiagram contains an Items collection. This member exposes a CollectionChanged event:http://es.infragistics.com/help/wpf/infragisticswpf4.controls.charts.xamdiagram.v16.2~infragistics.controls.charts.diagramitemscollection_members Eg.
Diagram.Items.CollectionChanged += Items_CollectionChanged;
You can handle the diagram's NodeMoving event, create a new Point for the node based on the old Y position and cancel it whenever the Node's Y attempts to change. I've tested this and the nodes move only in a horizontal direction.
private void Diagram_NodeMoving(object sender, DiagramNodeMovingEventArgs e) { if (e.OldPosition.Y != e.NewPosition.Y) { Point nodePoint = new Point(e.NewPosition.X, e.OldPosition.Y); e.Node.Position = nodePoint; e.Cancel = true; } }