Skip to content

Commit 6225680

Browse files
ES-976509 - Resolve Issues in Public Syncfusion Code Examples for DataGrid XAML Controls - Phase 2
1 parent 0c9c0fb commit 6225680

File tree

1 file changed

+63
-2
lines changed

1 file changed

+63
-2
lines changed

README.md

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,63 @@
1-
# How to serialize template column content in winui datagrid?
2-
This example describes how to serialize template column content in winui datagrid.
1+
# How to serialize template column content in WinUI DataGrid?
2+
3+
This example describes how to serialize template column content in **WinUI DataGrid**.
4+
5+
By default, you cannot serialize the template content in [WinUI DataGrid](https://www.syncfusion.com/winui-controls/datagrid) (SfDataGrid). This is the default behavior during Serialization and Deserialization operation.
6+
7+
### XAML
8+
9+
``` xml
10+
<Application.Resources>
11+
<ResourceDictionary>
12+
<ResourceDictionary.MergedDictionaries>
13+
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
14+
<!-- Other merged dictionaries here -->
15+
</ResourceDictionary.MergedDictionaries>
16+
<!-- Other app resources here -->
17+
<DataTemplate x:Key="cellTemplate">
18+
<Button Width="110" Content="{Binding Value}" />
19+
</DataTemplate>
20+
</ResourceDictionary>
21+
</Application.Resources>
22+
23+
24+
<dataGrid:SfDataGrid x:Name="dataGrid"
25+
AutoGenerateColumns="False"
26+
ItemsSource="{Binding Orders}">
27+
<dataGrid:SfDataGrid.Columns>
28+
<dataGrid:GridTemplateColumn CellTemplate="{StaticResource cellTemplate}"
29+
HeaderText="Order ID"
30+
MappingName="OrderID"
31+
SetCellBoundValue="True" />
32+
</dataGrid:SfDataGrid.Columns>
33+
</dataGrid:SfDataGrid>
34+
```
35+
36+
If you want to serialize and deserialize the template content, you have to reconstruct the same template during deserialization in [RestoreColumnProperties](https://help.syncfusion.com/cr/winui/Syncfusion.UI.Xaml.DataGrid.Serialization.SerializationController.html#Syncfusion_UI_Xaml_DataGrid_Serialization_SerializationController_RestoreColumnProperties_Syncfusion_UI_Xaml_DataGrid_Serialization_SerializableGridColumn_Syncfusion_UI_Xaml_DataGrid_GridColumn_) method.
37+
38+
### C#
39+
40+
``` csharp
41+
this.dataGrid.SerializationController = new SerializationControllerExt(this.dataGrid);
42+
43+
public class SerializationControllerExt : SerializationController
44+
{
45+
public SerializationControllerExt(SfDataGrid grid)
46+
: base(grid)
47+
{
48+
49+
}
50+
51+
protected override void RestoreColumnProperties(SerializableGridColumn serializableColumn, GridColumn column)
52+
{
53+
base.RestoreColumnProperties(serializableColumn, column);
54+
if (column is GridTemplateColumn)
55+
{
56+
if (column.MappingName == "OrderID")
57+
{
58+
column.CellTemplate = Application.Current.Resources["cellTemplate"] as DataTemplate;
59+
}
60+
}
61+
}
62+
}
63+
```

0 commit comments

Comments
 (0)