Skip to content

Commit fe293ac

Browse files
Merge pull request #1604 from syncfusion-content/WPF-925462-Stencil_And_SymbolGroup_Save_Load_Content_HotFix
WPF-925462-Stencil And SymbolGroup Save Load Content HotFix
2 parents c52262c + 076f3bf commit fe293ac

File tree

3 files changed

+114
-2
lines changed

3 files changed

+114
-2
lines changed

wpf-toc.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,7 @@
885885
<li><a href="/wpf/Diagram/Stencil/Appearance">Appearance</a></li>
886886
<li><a href="/wpf/Diagram/Stencil/ContextMenu">Context Menu</a></li>
887887
<li><a href="/wpf/Diagram/Stencil/Commands">Commands</a></li>
888+
<li><a href="/wpf/Diagram/Stencil/Serialization">Serialization</a></li>
888889
</ul>
889890
</li>
890891
<li><a href="/wpf/Diagram/Printing">Printing</a></li>

wpf/Diagram/Stencil/Appearance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ The stencil view can be toggled between the expanded and compact modes by clicki
178178

179179
|DisplayMode|Description|Output|
180180
|----------|-----------|--------|
181-
| Compact | The stencil always shows as a narrow sliver which can be opened to full width |![Expanded](Stencil_images/ExpandModeExpand.PNG) |
182-
| Expanded | Specifies to update the Expanded state of the stencil |![Symbol](Stencil_images/Stencil_Compact.PNG)|
181+
| Compact | The stencil always shows as a narrow sliver which can be opened to full width |![Symbol](Stencil_images/Stencil_Compact.PNG)|
182+
| Expanded | Specifies to update the Expanded state of the stencil |![Expanded](Stencil_images/ExpandModeExpand.PNG)|
183183

184184
You can show or hide the expander icon by using the [`ShowDisplayModeToggleButton`](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.Stencil.html#Syncfusion_UI_Xaml_Diagram_Stencil_Stencil_ShowDisplayModeToggleButton) property of the `Stencil` class.
185185

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
layout: post
3+
title: Stencil Serialization in WPF Diagram | Syncfusion®
4+
description: Learn how to serialize and deserialize stencils in Syncfusion WPF Diagram (SfDiagram) control, including individual symbol groups.
5+
platform: wpf
6+
control: SfDiagram
7+
documentation: ug
8+
---
9+
10+
# Stencil Serialization in WPF Diagram (SfDiagram)
11+
12+
[Stencil](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.html) serialization is the process of converting the state of stencil into a stream of bytes to recreate them when needed. Such streams can be stored in a database, as a file, or in memory. The reverse process is called deserialization.
13+
14+
### Saving the Stencil
15+
16+
In Stencil, [DataContractSerializer](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.serialization.datacontractserializer?view=net-8.0) is used for serialization. It allows you to serialize and save your stencil into a stream. Here is a simple code example showing how to save the stencil:
17+
18+
{% tabs %}
19+
{% highlight C# %}
20+
21+
// To Save as stream in file
22+
SaveFileDialog dialog = new SaveFileDialog();
23+
dialog.Title = "Save XAML";
24+
dialog.Filter = "XAML File (*.xaml)|*.xaml";
25+
if (dialog.ShowDialog() == true)
26+
{
27+
using (Stream str = File.Open(dialog.FileName, FileMode.OpenOrCreate))
28+
{
29+
stencil.Save(str);
30+
}
31+
}
32+
33+
// To Save as memory stream
34+
MemoryStream str = new MemoryStream();
35+
stencil.Save(str);
36+
37+
{% endhighlight %}
38+
{% endtabs %}
39+
40+
### Loading the Stencil
41+
42+
On deserialization, the saved stream is used to load the Stencil. We can continue using previously saved stencil by loading the saved stream. Here is a simple code example showing how to load the stencil:
43+
44+
{% tabs %}
45+
{% highlight C# %}
46+
47+
// Load from saved XAML file
48+
OpenFileDialog dialog = new OpenFileDialog();
49+
if (dialog.ShowDialog() == true)
50+
{
51+
using (Stream myStream = dialog.OpenFile())
52+
{
53+
stencil.Load(myStream);
54+
}
55+
}
56+
57+
// Load from saved memory stream
58+
myStream.Position = 0;
59+
stencil.Load(myStream);
60+
61+
{% endhighlight %}
62+
{% endtabs %}
63+
64+
## Exporting and Importing Symbol Group in Stencil
65+
66+
The Stencil also supports exporting and importing specific symbol groups. This functionality is useful when you need to save and reuse only certain symbol groups.
67+
68+
### Exporting a Symbol Group
69+
70+
In Stencil, the [ExportGroup](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.Stencil.html#Syncfusion_UI_Xaml_Diagram_Stencil_Stencil_ExportGroup_System_IO_Stream_System_String___) method allows you to export SymbolGroups. By providing the symbol group names as parameters to this method, you can save the specified SymbolGroups to a stream, preserving all their properties for easy sharing or future use. Here is a simple code example showing how to export the symbol groups:
71+
72+
{% tabs %}
73+
{% highlight C# %}
74+
75+
string[] symbolGroupNames = { "Basic Shapes", "Flow Shapes", "BPMN Editor Shapes" };
76+
77+
SaveFileDialog dialog = new SaveFileDialog();
78+
dialog.Title = "Save XAML";
79+
dialog.Filter = "XAML File (*.xaml)|*.xaml";
80+
if (dialog.ShowDialog() == true)
81+
{
82+
using (Stream s = File.Open(dialog.FileName, FileMode.OpenOrCreate))
83+
{
84+
stencil.ExportGroup(s, symbolGroupNames);
85+
}
86+
}
87+
88+
{% endhighlight %}
89+
{% endtabs %}
90+
91+
### Importing a Symbol Group
92+
93+
To import the saved symbol groups back into the stencil, use the [ImportGroup](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Diagram.Stencil.Stencil.html#Syncfusion_UI_Xaml_Diagram_Stencil_Stencil_ImportGroup_System_IO_Stream_) method with the saved stream. This will load the stencil with the saved symbol groups. Here is a simple code example showing how to import the symbol groups:
94+
95+
{% tabs %}
96+
{% highlight C# %}
97+
98+
OpenFileDialog dialog = new OpenFileDialog();
99+
if (dialog.ShowDialog() == true)
100+
{
101+
using (Stream myStream = dialog.OpenFile())
102+
{
103+
stencil.ImportGroup(myStream);
104+
}
105+
}
106+
107+
{% endhighlight %}
108+
{% endtabs %}
109+
110+
111+
[View Sample in GitHub](https://github.com/SyncfusionExamples/WPF-Diagram-Examples/tree/master/Samples/Stencil/SymbolGroupSerialize)

0 commit comments

Comments
 (0)