|
| 1 | +--- |
| 2 | +title: Binding to Collection |
| 3 | +page_title: .NET MAUI TreeDataGrid Documentation - Data Binding |
| 4 | +description: Learn how to bind the Telerik .NET MAUI TreeDataGrid to a collection of items and configure the data bindings for its columns. |
| 5 | +position: 1 |
| 6 | +slug: treedatagrid-data-binding |
| 7 | +tags: binding, collection, data, dotnet maui, maui, datagrid, enumerable |
| 8 | +--- |
| 9 | + |
| 10 | +# .NET MAUI TreeDataGrid Data Binding: Binding to a Collection |
| 11 | + |
| 12 | +This article lists the source types supported by the [Telerik UI for .NET MAUI TreeDataGrid]({%slug treedatagrid-overview%}). It guides you through the process of binding the DataGrid control to a collection of items and configuring the data binding for its columns. |
| 13 | + |
| 14 | +## Supported Collection Sources |
| 15 | + |
| 16 | +To populate the DataGrid with data, use the `ItemsSource` property of the control. For compatibility reasons, the `ItemsSource` property of the .NET MAUI DataGrid is declared as a `System.Object` type. However, using collections that implement the `IEnumerable` interface is recommended as it gives you more flexibility. |
| 17 | + |
| 18 | +## Binding to an ObservableCollection |
| 19 | + |
| 20 | +When you bind the `RadTreeDataGrid` to a collection that implements the `INotifyCollectionChanged` interface, the TreeDataGrid reflects and displays all changes to that collection, for example, any adding or removing of data items. The .NET MAUI framework implements the `INotifyCollectionChanged` interface in the `ObservableCollection<T>` class. |
| 21 | + |
| 22 | +Implementations of the `System.ComponentModel.ICollection` interface are also fully supported. When using such collection sources, the `RadTreeDataGrid` automatically picks up any sort, or filter descriptions defined in the collection and uses them to display the data. This makes the `System.ComponentModel.ICollection` interface implementations the recommended collection source for the TreeDataGrid. To display the hierarchy of the items define the [`TreeDataGridItemDescriptor`]({%slug treedatagrid-descriptor%}). |
| 23 | + |
| 24 | +The next example demonstrates how to bind the TreeDataGrid to an `ObservableCollection`. |
| 25 | + |
| 26 | +For this example, the TreeDataGrid control is bound to a collection of `Data` objects. |
| 27 | + |
| 28 | +**1.** Add a TreeDataGrid control to your page. |
| 29 | + |
| 30 | +```XAML |
| 31 | +<telerik:RadTreeDataGrid ItemsSource="{Binding Items}" x:Name="tree" |
| 32 | + AutoGenerateColumns="False"> |
| 33 | + <telerik:RadTreeDataGrid.ItemDescriptors> |
| 34 | + <telerik:TreeDataGridItemDescriptor ChildrenBinding="{Binding Children}" /> |
| 35 | + </telerik:RadTreeDataGrid.ItemDescriptors> |
| 36 | + <telerik:RadTreeDataGrid.BindingContext> |
| 37 | + <local:ViewModel /> |
| 38 | + </telerik:RadTreeDataGrid.BindingContext> |
| 39 | +</telerik:RadTreeDataGrid> |
| 40 | +``` |
| 41 | + |
| 42 | +**2.** Add the `telerik` namespace: |
| 43 | + |
| 44 | +```XAML |
| 45 | +xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui" |
| 46 | +``` |
| 47 | + |
| 48 | +**3.** Create a sample `Data` class: |
| 49 | + |
| 50 | +<snippet id='treedatagrid-data-model' /> |
| 51 | + |
| 52 | +**4.** Add the `ViewModel` class: |
| 53 | + |
| 54 | +<snippet id='treedatagrid-viewmodel' /> |
| 55 | + |
| 56 | +## Binding the Columns |
| 57 | + |
| 58 | +By default, TreeDataGrid generates typed columns automatically based on the public properties of the underlying data objects. When, for example, you set the `ItemsSource` of the `RadTreeDataGrid` to a collection of data (see the sample above), the control will create a separate column for each public property of the `Data` object. |
| 59 | + |
| 60 | +To disable the automatic generation of TreeDataGrid columns and manually define them, set the control's `AutoGenerateColumns` property to `False` and manually populate the `Columns` collection. |
| 61 | + |
| 62 | +**1.** Add a TreeDataGrid control to your page. |
| 63 | + |
| 64 | +<snippet id='treedatagrid-getting-started' /> |
| 65 | + |
| 66 | +**2.** Add the `telerik` namespace: |
| 67 | + |
| 68 | +```XAML |
| 69 | +xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui" |
| 70 | +``` |
| 71 | + |
| 72 | +**3.** Create a sample `Data` class: |
| 73 | + |
| 74 | +<snippet id='treedatagrid-data-model' /> |
| 75 | + |
| 76 | +**4.** Add the `ViewModel` class: |
| 77 | + |
| 78 | +<snippet id='treedatagrid-viewmodel' /> |
| 79 | + |
| 80 | +> To learn more about defining columns and the different types of columns, you can take a look at the [Columns Section]({%slug treedatagrid-columns-overview%}). |
| 81 | +
|
| 82 | +## Additional Resources |
| 83 | + |
| 84 | +- [.NET MAUI TreeDataGrid Product Page](https://www.telerik.com/maui-ui/treedatagrid) |
| 85 | +- [.NET MAUI TreeDataGrid Forum Page](https://www.telerik.com/forums/maui?tagId=1801) |
| 86 | +- [Telerik .NET MAUI Blogs](https://www.telerik.com/blogs/mobile-net-maui) |
| 87 | +- [Telerik .NET MAUI Roadmap](https://www.telerik.com/support/whats-new/maui-ui/roadmap) |
| 88 | + |
| 89 | +## See Also |
| 90 | + |
| 91 | +- [Filtering UI in the Telerik UI for .NET MAUI TreeDataGrid]({%slug treedatagrid-filtering-ui%}) |
| 92 | +- [Programmatic Filtering in the TreeDataGrid]({%slug treedatagrid-programmatic-filtering%}) |
| 93 | +- [Styling the Appearance of the TreeDataGrid]({%slug treedatagrid-styling%}) |
0 commit comments