Class ElementCollection
An observable collection of canvas elements with typed accessors and lookup methods.
public class ElementCollection : ObservableCollection<ICanvasElement>, IList<ICanvasElement>, ICollection<ICanvasElement>, IReadOnlyList<ICanvasElement>, IReadOnlyCollection<ICanvasElement>, IEnumerable<ICanvasElement>, IList, ICollection, IEnumerable, INotifyCollectionChanged, INotifyPropertyChanged
- Inheritance
-
ElementCollection
- Implements
- Inherited Members
Remarks
This collection is the core data structure for the canvas-first architecture. It holds all elements (nodes, edges, shapes, etc.) and provides efficient lookup and filtering methods.
Supports batch operations via AddRange(IEnumerable<ICanvasElement>) for efficient bulk updates.
Properties
ByZIndex
Gets all elements sorted by Z-index (rendering order).
public IEnumerable<ICanvasElement> ByZIndex { get; }
Property Value
EdgeCount
Gets the count of edges in the collection. O(1) operation.
public int EdgeCount { get; }
Property Value
Edges
Gets all edge elements in the collection. PERFORMANCE: Returns cached list for O(1) access instead of O(n) OfType filtering.
public IReadOnlyList<Edge> Edges { get; }
Property Value
NodeCount
Gets the count of nodes in the collection. O(1) operation.
public int NodeCount { get; }
Property Value
Nodes
Gets all node elements in the collection. PERFORMANCE: Returns cached list for O(1) access instead of O(n) OfType filtering.
public IReadOnlyList<Node> Nodes { get; }
Property Value
Selected
Gets all selected elements.
public IEnumerable<ICanvasElement> Selected { get; }
Property Value
ShapeCount
Gets the count of shapes in the collection. O(1) operation.
public int ShapeCount { get; }
Property Value
Shapes
Gets all shape elements in the collection. PERFORMANCE: Returns cached list for O(1) access instead of O(n) OfType filtering.
public IReadOnlyList<ShapeElement> Shapes { get; }
Property Value
Visible
Gets all visible elements.
public IEnumerable<ICanvasElement> Visible { get; }
Property Value
Methods
AddRange(IEnumerable<ICanvasElement>)
Adds a range of elements without firing individual notifications. A single Reset notification is fired after all elements are added.
public void AddRange(IEnumerable<ICanvasElement> elements)
Parameters
elementsIEnumerable<ICanvasElement>The elements to add.
ClearItems()
Removes all items from the collection.
protected override void ClearItems()
ContainsId(string)
Determines whether an element with the specified ID exists.
public bool ContainsId(string id)
Parameters
idstringThe element ID to check.
Returns
FindById(string)
Finds an element by its ID.
public ICanvasElement? FindById(string id)
Parameters
idstringThe element ID to search for.
Returns
- ICanvasElement
The element if found; otherwise, null.
FindById<T>(string)
Finds an element by its ID and casts to the specified type.
public T? FindById<T>(string id) where T : class, ICanvasElement
Parameters
idstringThe element ID to search for.
Returns
- T
The element if found and of the correct type; otherwise, null.
Type Parameters
TThe expected element type.
FindInBounds(Rect, bool)
Finds all elements within the specified bounds.
public IEnumerable<ICanvasElement> FindInBounds(Rect bounds, bool includePartial = false)
Parameters
boundsRectThe bounds to search within.
includePartialboolIf true, includes elements that partially intersect the bounds.
Returns
GetEdgesForNode(string)
Gets all edges connected to a specific node (either as source or target). PERFORMANCE: O(1) lookup using edge index instead of O(n) iteration.
public IEnumerable<Edge> GetEdgesForNode(string nodeId)
Parameters
nodeIdstringThe ID of the node to find edges for.
Returns
- IEnumerable<Edge>
Enumerable of edges connected to the node.
GetEdgesForNodes(IEnumerable<string>)
Gets all edges connected to any of the specified nodes. PERFORMANCE: O(k) where k is number of connected edges, instead of O(n) iteration over all edges.
public List<Edge> GetEdgesForNodes(IEnumerable<string> nodeIds)
Parameters
nodeIdsIEnumerable<string>The IDs of nodes to find edges for.
Returns
InsertItem(int, ICanvasElement)
Inserts an item into the collection at the specified index.
protected override void InsertItem(int index, ICanvasElement item)
Parameters
indexintThe zero-based index at which
itemshould be inserted.itemICanvasElementThe object to insert.
OfElementType<T>()
Gets all elements of the specified type.
public IEnumerable<T> OfElementType<T>() where T : ICanvasElement
Returns
- IEnumerable<T>
Type Parameters
TThe element type to filter by.
OnCollectionChanged(NotifyCollectionChangedEventArgs)
Raises the CollectionChanged event with the provided arguments.
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
Parameters
eNotifyCollectionChangedEventArgsArguments of the event being raised.
RemoveItem(int)
Removes the item at the specified index of the collection.
protected override void RemoveItem(int index)
Parameters
indexintThe zero-based index of the element to remove.
RemoveRange(IEnumerable<ICanvasElement>)
Removes a range of elements without firing individual notifications. A single Reset notification is fired after all elements are removed.
public void RemoveRange(IEnumerable<ICanvasElement> elements)
Parameters
elementsIEnumerable<ICanvasElement>The elements to remove.
ReplaceAll(IEnumerable<ICanvasElement>)
Clears all elements and adds the specified elements.
public void ReplaceAll(IEnumerable<ICanvasElement> elements)
Parameters
elementsIEnumerable<ICanvasElement>The elements to set.
SetItem(int, ICanvasElement)
Replaces the element at the specified index.
protected override void SetItem(int index, ICanvasElement item)
Parameters
indexintThe zero-based index of the element to replace.
itemICanvasElementThe new value for the element at the specified index.