|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: Diagram - Events |
| 4 | +description: Learn about the Blazor Diagram component events and experiment with them in the provided runnable code examples. |
| 5 | +slug: diagram-events |
| 6 | +tags: telerik,blazor,diagram |
| 7 | +published: True |
| 8 | +position: 100 |
| 9 | +--- |
| 10 | + |
| 11 | +# Blazor Diagram Events |
| 12 | + |
| 13 | +The Telerik Blazor Diagram fires events that are related to different user actions. This article describes all these events and their event arguments: |
| 14 | + |
| 15 | +* [`OnConnectionClick`](#onconnectionclick) |
| 16 | +* [`OnShapeClick`](#onshapeclick) |
| 17 | + |
| 18 | +## OnConnectionClick |
| 19 | + |
| 20 | +The `OnConnectionClick` event fires when the user clicks on a connection, including the connection ends that rest on the shape boundaries. The event argument is of type [`DiagramConnectionClickEventArgs`](slug:Telerik.Blazor.Components.DiagramConnectionClickEventArgs) and it provides information about the linked shapes (if they exist) or about the connection coordinates (if set). |
| 21 | + |
| 22 | +>caption Using the Diagram OnConnectionClick event |
| 23 | +
|
| 24 | +````RAZOR.skip-repl |
| 25 | +<TelerikDiagram OnConnectionClick="@OnDiagramConnectionClick" /> |
| 26 | +
|
| 27 | +@code { |
| 28 | + private void OnDiagramConnectionClick(DiagramConnectionClickEventArgs args) |
| 29 | + { |
| 30 | +
|
| 31 | + } |
| 32 | +} |
| 33 | +```` |
| 34 | + |
| 35 | +Also see the [example](#example) below. |
| 36 | + |
| 37 | +## OnShapeClick |
| 38 | + |
| 39 | +The `OnShapeClick` event fires when the user clicks on a shape. The event argument is of type [`DiagramShapeClickEventArgs`](slug:Telerik.Blazor.Components.DiagramShapeClickEventArgs) and provides the shape `Id`. |
| 40 | + |
| 41 | +>caption Using the Diagram OnShapeClick event |
| 42 | +
|
| 43 | +````RAZOR.skip-repl |
| 44 | +<TelerikDiagram OnShapeClick="@OnDiagramShapeClick" /> |
| 45 | +
|
| 46 | +@code { |
| 47 | + private void OnDiagramShapeClick(DiagramShapeClickEventArgs args) |
| 48 | + { |
| 49 | +
|
| 50 | + } |
| 51 | +} |
| 52 | +```` |
| 53 | + |
| 54 | +## Example |
| 55 | + |
| 56 | +The following example demonstrates all Diagram events in action. |
| 57 | + |
| 58 | +>caption Using Diagram events |
| 59 | +
|
| 60 | +````RAZOR |
| 61 | +<TelerikDiagram Height="360px" |
| 62 | + OnConnectionClick="@OnDiagramConnectionClick" |
| 63 | + OnShapeClick="@OnDiagramShapeClick"> |
| 64 | + <DiagramLayout Type="@DiagramLayoutType.Tree" /> |
| 65 | +
|
| 66 | + <DiagramShapes> |
| 67 | + <DiagramShape Id="shape1"> |
| 68 | + <DiagramShapeContent Text="Shape 1"> |
| 69 | + </DiagramShapeContent> |
| 70 | + </DiagramShape> |
| 71 | + <DiagramShape Id="shape2"> |
| 72 | + <DiagramShapeContent Text="Shape 2"> |
| 73 | + </DiagramShapeContent> |
| 74 | + </DiagramShape> |
| 75 | + <DiagramShape Id="shape3"> |
| 76 | + <DiagramShapeContent Text="Shape 3"> |
| 77 | + </DiagramShapeContent> |
| 78 | + </DiagramShape> |
| 79 | + </DiagramShapes> |
| 80 | +
|
| 81 | + <DiagramConnections> |
| 82 | + <DiagramConnection FromId="shape1" ToId="shape2" /> |
| 83 | + <DiagramConnection FromId="shape1" ToId="shape3" /> |
| 84 | + <DiagramConnection> |
| 85 | + <DiagramConnectionFrom X="300" Y="20" /> |
| 86 | + <DiagramConnectionTo X="400" Y="200" /> |
| 87 | + </DiagramConnection> |
| 88 | + </DiagramConnections> |
| 89 | +</TelerikDiagram> |
| 90 | +
|
| 91 | +@DiagramEventLog |
| 92 | +
|
| 93 | +@code { |
| 94 | + private string DiagramEventLog { get; set; } = string.Empty; |
| 95 | +
|
| 96 | + private void OnDiagramConnectionClick(DiagramConnectionClickEventArgs args) |
| 97 | + { |
| 98 | + if (args.FromX != null) |
| 99 | + { |
| 100 | + DiagramEventLog = $"Clicked on the connection between coordinates ({args.FromX}, {args.FromY}) and ({args.ToX}, {args.ToY})."; |
| 101 | + } |
| 102 | + else |
| 103 | + { |
| 104 | + DiagramEventLog = $"Clicked on the connection between shapes '{args.FromId}' and '{args.ToId}'."; |
| 105 | + } |
| 106 | + } |
| 107 | +
|
| 108 | + private void OnDiagramShapeClick(DiagramShapeClickEventArgs args) |
| 109 | + { |
| 110 | + DiagramEventLog = $"Clicked on shape '{args.Id}'."; |
| 111 | + } |
| 112 | +} |
| 113 | +```` |
| 114 | + |
| 115 | +## See Also |
| 116 | + |
| 117 | +* [Live Demos: Diagram](https://demos.telerik.com/blazor-ui/diagram/overview) |
| 118 | +* [Diagram API Reference](slug:Telerik.Blazor.Components.TelerikDiagram) |
0 commit comments