Skip to content

Controlled vs Uncontrolled

The canvas always edits the lists it is given in place; what differs is whether the page is told. Controlled, the page handles NodesChanged and EdgesChanged and keeps its own view of the graph current after every edit. Uncontrolled, it handles nothing and learns what changed only when it asks the canvas.

The page knows: 3 nodes, 1 edges, Node 1 at (160, 40) - as the page built it
Node 1
Node 2
Node 3
bit Blow

Try it

  • With Controlled ticked, drag Node 1: the readout updates when you let go, not while the node moves.
  • Drag from a handle to another node to connect them, or select something and press Delete: the counts follow each edit.
  • Untick Controlled and do the same: the canvas still works, but the readout stays where it was - the page was not told.
  • Press Ask the canvas to read the graph as it is now. Ticking Controlled again also catches up, since the page reads the graph on taking control.

How it works

  • In both modes Nodes and Edges are the page's own lists, and the canvas adds, moves and removes items in them directly: nothing is copied, so there is no second graph to fall out of step.
  • Controlled hands the canvas NodesChanged and EdgesChanged handlers. They are raised once an edit is complete - a drag released, a connection made, something deleted, pasted or nudged with the keys - with the list itself, and the page re-renders after each.
  • Uncontrolled passes default for both callbacks, so nothing is raised and the page's copy of the counts goes stale. Ask the canvas reads the graph through the component reference with ToObject(deep: false), a BlowSnapshot of the live nodes and edges, without copying them.
  • Nodes/NodesChanged and Edges/EdgesChanged are bindable pairs, so @bind-Nodes="_nodes" compiles too; since the list instance never changes, the binding is mostly a signal to persist.
  • For the kind of edit as well as the fact of one - who made it and what it touched - handle OnGraphChanged instead; /examples/grouping logs it.
An unhandled error has occurred. Reload 🗙