Skip to content

SyncfusionExamples/json-data-binding-for-.net-maui-treeview

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 

Repository files navigation

-json-data-binding-for-.net-maui-treeview

This repository demonstrates how to bind JSON data to the .NET MAUI TreeView (SfTreeView) control. It provides a sample implementation that downloads and deserializes hierarchical JSON data, maps it to data models, and binds it to the TreeView using the ItemsSource property for dynamic and structured data display.

Sample

The Syncfusion® .NET MAUI TreeView enables you to bind data from a JSON file using the ItemsSource property. This demonstration will guide you through the process of binding JSON Collection to the TreeView.

XAML

<ContentPage.BindingContext>
    <local:TreeViewViewModel x:Name="viewModel"/>
</ContentPage.BindingContext>
<syncfusion:SfTreeView x:Name="treeView" Margin="10"
                        ItemTemplateContextType="Node"                          
                        ItemsSource="{Binding JSONCollection}"                           
                        ChildPropertyName="SubTaskDetails">
    <syncfusion:SfTreeView.Behaviors>
        <local:TreeViewBehavior/>
    </syncfusion:SfTreeView.Behaviors>
</syncfusion:SfTreeView>

View Model

public class TreeViewViewModel : INotifyPropertyChanged
{
    private bool isDownloaded;
    private IList<TaskDetails> jsonCollection;

    public event PropertyChangedEventHandler? PropertyChanged;
    #region Fields

    public IList<TaskDetails> JSONCollection 
    {
        get
        {
            return jsonCollection;
        }
        set
        {
            jsonCollection = value;
            OnPropertyChanged(nameof(JSONCollection));
        }
    }

    public DataServices DataService { get; set; }

    #endregion
    #region Constructor
    public TreeViewViewModel()
    {
        JSONCollection = new ObservableCollection<TaskDetails>();
        DataService = new DataServices();
        GenerateSource();
    }
    #endregion

    #region Generate Source
    private async void GenerateSource()
    {
        isDownloaded = await DataService.DownloadJsonAsync();
        if (isDownloaded)
        {
            var localFolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
            var fileText = File.ReadAllText(Path.Combine(localFolder, "Data.json"));

            //Read data from the local path and set it to the collection bound to the ListView.
            JSONCollection = JsonConvert.DeserializeObject<IList<TaskDetails>>(fileText);                
        }
    }

    #endregion
}

Requirements to run the demo

To run the demo, refer to System Requirements for .NET MAUI.

Troubleshooting:

Path too long exception

If you are facing path too long exception when building this example project, close Visual Studio and rename the repository to short and build the project.

License

Syncfusion® has no liability for any damage or consequence that may arise from using or viewing the samples. The samples are for demonstrative purposes. If you choose to use or access the samples, you agree to not hold Syncfusion® liable, in any form, for any damage related to use, for accessing, or viewing the samples. By accessing, viewing, or seeing the samples, you acknowledge and agree Syncfusion®'s samples will not allow you seek injunctive relief in any form for any claim related to the sample. If you do not agree to this, do not view, access, utilize, or otherwise do anything with Syncfusion®'s samples.

About

This example explains about how to bind json data to .net maui treeview.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 7

Languages