Class Graph
Represents a flow graph containing nodes, edges, and other canvas elements. Provides methods for adding, removing, and querying graph elements.
public class Graph
- Inheritance
-
Graph
- Inherited Members
- Extension Methods
Remarks
The Graph class uses the canvas-first architecture where the Elements collection is the single source of truth. All element types (nodes, edges, shapes, etc.) are stored in this unified collection.
Nodes and Edges provide typed read-only views into Elements for convenience when working with graph-specific operations.
Subscribe to Elements.CollectionChanged for all element changes, or use the convenience events NodesChanged and EdgesChanged for type-specific notifications.
Constructors
Graph()
Initializes a new instance of the Graph class.
public Graph()
Properties
Edges
Gets a read-only view of all edge elements.
public IReadOnlyList<Edge> Edges { get; }
Property Value
Remarks
This is a live view into Elements. To add/remove edges, use AddEdge(Edge), AddEdges(IEnumerable<Edge>), RemoveEdge(string), or modify Elements directly.
Elements
Gets the collection of all canvas elements (nodes, edges, shapes, etc.). This is the single source of truth for all graph elements.
public ElementCollection Elements { get; }
Property Value
Remarks
Subscribe to Elements.CollectionChanged for change notifications.
Use typed accessors Nodes and Edges for read-only typed access.
IsBatchLoading
Gets whether the graph is currently in batch loading mode. During batch loading, edge validation is skipped.
public bool IsBatchLoading { get; }
Property Value
Nodes
Gets a read-only view of all node elements.
public IReadOnlyList<Node> Nodes { get; }
Property Value
Remarks
This is a live view into Elements. To add/remove nodes, use AddNode(Node), AddNodes(IEnumerable<Node>), RemoveNode(string), or modify Elements directly.
Methods
AddEdge(Edge)
Adds an edge to the graph.
public void AddEdge(Edge edge)
Parameters
edgeEdgeThe edge to add.
Exceptions
- InvalidOperationException
Thrown if source or target node doesn't exist (unless in batch loading mode).
AddEdges(IEnumerable<Edge>)
Adds multiple edges at once with a single notification.
public void AddEdges(IEnumerable<Edge> edges)
Parameters
edgesIEnumerable<Edge>The edges to add.
AddElement(ICanvasElement)
Adds any canvas element to the graph.
public void AddElement(ICanvasElement element)
Parameters
elementICanvasElementThe element to add.
AddElements(IEnumerable<ICanvasElement>)
Adds multiple elements at once with a single notification.
public void AddElements(IEnumerable<ICanvasElement> elements)
Parameters
elementsIEnumerable<ICanvasElement>The elements to add.
AddNode(Node)
Adds a node to the graph.
public void AddNode(Node node)
Parameters
nodeNodeThe node to add.
AddNodes(IEnumerable<Node>)
Adds multiple nodes at once with a single notification.
public void AddNodes(IEnumerable<Node> nodes)
Parameters
nodesIEnumerable<Node>The nodes to add.
BeginBatchLoad()
Begins batch loading mode. Edge validation is skipped until EndBatchLoad is called. Use this when adding many elements at once.
public void BeginBatchLoad()
ClearSelection()
Clears the selection of all nodes and edges.
public void ClearSelection()
ContainsEdge(string)
Determines whether an edge with the specified ID exists.
public bool ContainsEdge(string edgeId)
Parameters
edgeIdstringThe edge ID to check.
Returns
ContainsNode(string)
Determines whether a node with the specified ID exists.
public bool ContainsNode(string nodeId)
Parameters
nodeIdstringThe node ID to check.
Returns
EndBatchLoad()
Ends batch loading mode and raises BatchLoadCompleted event.
public void EndBatchLoad()
FindEdge(string)
Finds an edge by its ID.
public Edge? FindEdge(string edgeId)
Parameters
edgeIdstringThe edge ID to search for.
Returns
- Edge
The edge if found; otherwise, null.
FindNode(string)
Finds a node by its ID.
public Node? FindNode(string nodeId)
Parameters
nodeIdstringThe node ID to search for.
Returns
- Node
The node if found; otherwise, null.
GetEdgesForNode(string)
Gets all edges connected to a node.
public IEnumerable<Edge> GetEdgesForNode(string nodeId)
Parameters
nodeIdstringThe node ID.
Returns
- IEnumerable<Edge>
Enumerable of connected edges.
GetSelectedEdges()
Gets all currently selected edges.
public IEnumerable<Edge> GetSelectedEdges()
Returns
- IEnumerable<Edge>
Enumerable of selected edges.
GetSelectedNodes()
Gets all currently selected nodes.
public IEnumerable<Node> GetSelectedNodes()
Returns
- IEnumerable<Node>
Enumerable of selected nodes.
RemoveEdge(string)
Removes an edge from the graph.
public void RemoveEdge(string edgeId)
Parameters
edgeIdstringThe ID of the edge to remove.
RemoveEdges(IEnumerable<string>)
Removes multiple edges at once.
public void RemoveEdges(IEnumerable<string> edgeIds)
Parameters
edgeIdsIEnumerable<string>The IDs of edges to remove.
RemoveElement(ICanvasElement)
Removes any canvas element from the graph.
public void RemoveElement(ICanvasElement element)
Parameters
elementICanvasElementThe element to remove.
RemoveElements(IEnumerable<ICanvasElement>)
Removes multiple elements at once with a single notification.
public void RemoveElements(IEnumerable<ICanvasElement> elements)
Parameters
elementsIEnumerable<ICanvasElement>The elements to remove.
RemoveNode(string)
Removes a node and all connected edges from the graph.
public void RemoveNode(string nodeId)
Parameters
nodeIdstringThe ID of the node to remove.
RemoveNodes(IEnumerable<string>)
Removes multiple nodes and their connected edges at once.
public void RemoveNodes(IEnumerable<string> nodeIds)
Parameters
nodeIdsIEnumerable<string>The IDs of nodes to remove.
SelectGroupChildren(string)
Selects all children of a group node.
public IReadOnlyList<Node> SelectGroupChildren(string groupId)
Parameters
groupIdstringThe ID of the group node.
Returns
- IReadOnlyList<Node>
The list of child nodes that were selected.
SelectNodesInArea(Rect, bool)
Selects all nodes that intersect with the specified rectangular area.
public IReadOnlyList<Node> SelectNodesInArea(Rect bounds, bool fullyContained = false)
Parameters
boundsRectThe rectangular area in canvas coordinates.
fullyContainedboolIf true, only selects nodes fully contained within the bounds. If false (default), selects nodes that intersect with the bounds.
Returns
- IReadOnlyList<Node>
The list of nodes that were selected.
UnselectNodesInArea(Rect, bool)
Unselects all nodes that intersect with the specified rectangular area.
public IReadOnlyList<Node> UnselectNodesInArea(Rect bounds, bool fullyContained = false)
Parameters
boundsRectThe rectangular area in canvas coordinates.
fullyContainedboolIf true, only unselects nodes fully contained within the bounds. If false (default), unselects nodes that intersect with the bounds.
Returns
- IReadOnlyList<Node>
The list of nodes that were unselected.
Events
BatchLoadCompleted
Event raised when batch loading completes. Subscribe to this to refresh UI after bulk operations.
public event EventHandler? BatchLoadCompleted
Event Type
EdgesChanged
Event raised when edges are added, removed, or the collection is reset. This is a convenience event derived from Elements.CollectionChanged.
public event NotifyCollectionChangedEventHandler? EdgesChanged
Event Type
NodeBoundsChanged
Event raised when any node's bounds (position or size) change. Uses lazy subscription - only subscribes to individual node events when this event has subscribers. Ideal for spatial index invalidation and layout systems.
public event EventHandler<NodeBoundsChangedEventArgs>? NodeBoundsChanged
Event Type
Remarks
This event uses a lazy subscription pattern for performance. When no handlers are subscribed, no per-node subscriptions are maintained. Once a handler subscribes, the Graph automatically subscribes to all existing nodes' BoundsChanged events and manages subscriptions as nodes are added/removed.
For high-frequency operations like dragging many nodes, consider using explicit invalidation methods instead of relying on this event, as it fires for each individual node change.
NodesChanged
Event raised when nodes are added, removed, or the collection is reset. This is a convenience event derived from Elements.CollectionChanged.
public event NotifyCollectionChangedEventHandler? NodesChanged
Event Type
ShapesChanged
Event raised when shapes are added, removed, or the collection is reset. This is a convenience event derived from Elements.CollectionChanged.
public event NotifyCollectionChangedEventHandler? ShapesChanged