You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: knowledge-base/diagram-change-shape-color-onshapeclick.md
+93-39Lines changed: 93 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,15 +22,17 @@ res_type: kb
22
22
23
23
## Description
24
24
25
-
I am using the Diagram `OnShapeClick` event to update the `DiagramShapeFill``Color` of the selected Diagram shape. However, this resets the positions of the shapes that the user has dragged. How to update the shape background colors while maintaining the current shape positions?
25
+
I am using the Diagram `OnShapeClick` event to update the `DiagramShapeFill``Color` of the selected Diagram shape. However, this resets the positions of the shapes that the user has dragged. How to update the shape background colors while maintaining the current shape positions? In addition, I want to persist the custom shape colors when the Diagram `Layout` changes.
26
26
27
27
## Cause
28
28
29
-
The Diagram `OnShapeClick` event handler is an `EventCallback` and triggers component re-render. If the [Diagram shapes are defined](slug:diagram-shapes#basics) without their `X` and `Y` properties and [shape dragging](slug:diagram-shapes#editability) is enabled, the component definition does not include the current shape positions. As a result, a re-render resets the shapes to their original places.
29
+
The Diagram `OnShapeClick` event handler is an `EventCallback` and triggers component re-render. If the [Diagram shapes are defined](slug:diagram-shapes#basics) without their `X` and `Y` properties and [shape dragging](slug:diagram-shapes#editability) is enabled, the Razor component definition does not include the current shape positions. As a result, a re-render resets the shapes to their original places.
30
+
31
+
Similarly, if the Diagram Shape background colors are not part of the Razor component declaration, they will be reset to the default value if the Diagram `Layout` changes.
30
32
31
33
## Solution
32
34
33
-
Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist the Diagram state through JSON](slug:diagram-overview#define-shapes-and-connections-in-json) on each re-render:
35
+
Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [define and persist the Diagram state through JSON](slug:diagram-overview#define-shapes-and-connections-in-json) on each render:
34
36
35
37
1. Implement classes with property names that correspond to the ones in the [Diagram JSON state](slug:diagram-overview#define-shapes-and-connections-in-json).
36
38
1. Subscribe to the [Diagram `OnShapeClick` event](slug:diagram-events#onshapeclick).
@@ -45,49 +47,33 @@ Use the Diagram `SaveAsJsonAsync` and `LoadFromJsonAsync` methods to [persist th
45
47
@using System.Text.Json
46
48
@using System.Text.Json.Serialization
47
49
48
-
<TelerikDiagram @ref="@DiagramRef"
49
-
Height="100vh"
50
-
OnShapeClick="@OnDiagramShapeClick"
51
-
Zoom="0.8">
52
-
<DiagramLayout Type="@DiagramLayoutType.Tree" />
50
+
Drag a shape and then click on a shape to change its background color. The shape positions will persist.
0 commit comments