|
| 1 | +--- |
| 2 | +title: Binding to Dynamic Data |
| 3 | +page_title: .NET MAUI TreeDataGrid Documentation - Dynamic Data |
| 4 | +description: Learn how to bind the Telerik UI for .NET MAUI TreeDataGrid to different types of dynamic data - expando and dynamic objects. |
| 5 | +position: 2 |
| 6 | +slug: treedatagrid-dynamic-data |
| 7 | +tags: binding, dynamic, data, expando, dynamic object, dotnet maui, maui, datagrid |
| 8 | +--- |
| 9 | + |
| 10 | +# .NET MAUI TreeDataGrid Data Binding: Binding to Dynamic Data |
| 11 | + |
| 12 | +As of Telerik UI for .NET MAUI 6.8.0, the [.NET MAUI TreeDataGrid]({%slug treedatagrid-overview%}) supports binding to any type that implements the standard `IDynamicMetaObjectProvider` DLR interface, such as `DynamicObject` and `ExpandoObject`. |
| 13 | + |
| 14 | +Using Dynamic Data enables you to filter and sort the data inside the DataGrid both through the UI and programmatically. |
| 15 | + |
| 16 | +## Executing Dynamic Data on iOS and MacCatalyst |
| 17 | + |
| 18 | +When you develop applications for Apple devices and plan to bind the DataGrid to dynamic data, you must consider the <a href = "https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0&source=recommendations" target="_blank">security restrictions set by Apple</a>. These restrictions disallow the execution of dynamically generated code on a device. As a result, apps that use `DynamicObject` or `ExpandoObject` will crash in a release build for iOS and Mac Catalyst with a `System.ExecutionEngineException`. |
| 19 | +To prevent the exception, use the `MtouchInterpreter` <a href = "https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0&source=recommendations#enable-the-interpreter" target="_blank"> as recommended by Microsoft</a>. |
| 20 | + |
| 21 | +```xml |
| 22 | +<PropertyGroup Condition="'$(Configuration)|$(RuntimeIdentifier)'=='Release|maccatalyst-arm64'"> |
| 23 | + <MtouchInterpreter>-all,+Telerik.Maui.Controls.dll</MtouchInterpreter> |
| 24 | +</PropertyGroup> |
| 25 | +``` |
| 26 | + |
| 27 | +For more information about these limitations and the suggested solution, see Microsoft's <a href = "https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0&source=recommendations" target="_blank">Mono interpreter on iOS and Mac Catalyst</a> article. |
| 28 | + |
| 29 | +## Binding to DynamicObject |
| 30 | + |
| 31 | +The following example shows how to implement a `DynamicObject` class with Dynamic Language Runtime (DLR) and Common Language Runtime (CLR) fields and bind it to the TreeDataGrid. The standard CLR properties registered in the dynamic type must be exposed through the DLR API. |
| 32 | + |
| 33 | +The model shows a class that derives from `DynamicObject` and contains a CLR property called `Id`. When the DataGrid auto-generates its columns, the `TryGetMember` method of the `DynamicObject` class will be used to fetch the values for each column. You must also implement a specific logic in the method to allow `RadTreeDataGrid` to work with both CLR and DLR data. |
| 34 | + |
| 35 | +**1.** Add the model for the TreeDataGrid: |
| 36 | + |
| 37 | +<snippet id='treedatagrid-dynamicobject-model' /> |
| 38 | + |
| 39 | +**2.** Define the `ViewModel`: |
| 40 | + |
| 41 | +<snippet id='treedatagrid-dynamicobject-viewmodel' /> |
| 42 | + |
| 43 | +**3.** Define the `RadTreeDataGrid`: |
| 44 | + |
| 45 | +<snippet id='treedatagrid-dynamicobject' /> |
| 46 | + |
| 47 | +**4.** Add the `telerik` namespace: |
| 48 | + |
| 49 | +```XAML |
| 50 | +xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui" |
| 51 | +``` |
| 52 | + |
| 53 | +This is the result: |
| 54 | + |
| 55 | + |
| 56 | +## Binding to ExpandoObject |
| 57 | + |
| 58 | +**1.** Define the `RadTreeDataGrid` control in XAML: |
| 59 | + |
| 60 | +<snippet id='treedatagrid-expandoobject' /> |
| 61 | + |
| 62 | +**2.** Add the `telerik` namespace: |
| 63 | + |
| 64 | +```XAML |
| 65 | +xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui" |
| 66 | +``` |
| 67 | + |
| 68 | +**3.** Define the `ViewModel`: |
| 69 | + |
| 70 | +<snippet id='treedatagrid-expandoobject-viewmodel' /> |
| 71 | + |
| 72 | +This is the result: |
| 73 | + |
| 74 | + |
| 75 | +## Additional Resources |
| 76 | + |
| 77 | +- [.NET MAUI TreeDataGrid Product Page](https://www.telerik.com/maui-ui/treedatagrid) |
| 78 | +- [.NET MAUI TreeDataGrid Forum Page](https://www.telerik.com/forums/maui?tagId=1801) |
| 79 | +- [Telerik .NET MAUI Blogs](https://www.telerik.com/blogs/mobile-net-maui) |
| 80 | +- [Telerik .NET MAUI Roadmap](https://www.telerik.com/support/whats-new/maui-ui/roadmap) |
| 81 | + |
| 82 | +## See Also |
| 83 | + |
| 84 | +- [Filtering UI in the Telerik UI for .NET MAUI TreeDataGrid]({%slug treedatagrid-filtering-ui%}) |
| 85 | +- [Programmatic Filtering in the TreeDataGrid]({%slug treedatagrid-programmatic-filtering%}) |
| 86 | +- [Styling the Appearance of the TreeDataGrid]({%slug treedatagrid-styling%}) |
0 commit comments