SfTreeView

Hi,

Please help me json string directly bind to sftreeview without Deserialize into model.

Regards,

Yogita Magdum 


1 Reply

LN Lakshmi Natarajan Syncfusion Team July 2, 2021 04:00 AM UTC

Hi Yogita, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “Please help me json string directly bind to sftreeview without Deserialize into model” from our side. We would like to inform you that you can bind the json string to SfTreeView in the UnboundMode. 
 
Please refer to our user guidance document to use Unbound mode, 
 
Please refer to the following code snippets for your requirement, 
private SfTreeView InitalizeTreeView() 
{ 
    var treeView = new SfTreeView(); 
 
    var assembly = typeof(MainPage).GetTypeInfo().Assembly; 
    Stream stream = assembly.GetManifestResourceStream("TreeViewXamarin.Data.navigation.json"); 
 
    List<string> jsonArray = null; 
             
    //Read Json data as list of string. 
    using (StreamReader sr = new StreamReader(stream)) 
    { 
        var jsonText = sr.ReadToEnd(); 
        jsonArray = JsonConvert.DeserializeObject<List<string>>(jsonText); 
    } 
 
    if (jsonArray != null) 
    { 
        var australia = new TreeViewNode() { Content = jsonArray[0], IsExpanded = true }; 
        var _NSW = new TreeViewNode() { Content = jsonArray[1] }; 
        var _Sydney = new TreeViewNode() { Content = jsonArray[2] }; 
        australia.ChildNodes.Add(_NSW); 
        _NSW.ChildNodes.Add(_Sydney); 
 
        var usa = new TreeViewNode() { Content = jsonArray[3], IsExpanded = true }; 
        var newYork = new TreeViewNode() { Content = jsonArray[4] }; 
        var _California = new TreeViewNode() { Content = jsonArray[5] }; 
        var SanFrancisco = new TreeViewNode() { Content = jsonArray[6] }; 
        usa.ChildNodes.Add(newYork); 
        usa.ChildNodes.Add(_California); 
        _California.ChildNodes.Add(SanFrancisco); 
        treeView.Nodes.Add(australia); 
        treeView.Nodes.Add(usa); 
    } 
 
    return treeView; 
} 
 
 
We have prepared a sample to bind json string to TreeView and attached in the following link, 
 
You can also refer to our online document to work with Unbound mode, 
KB links:  
 
Note: We cannot bind items to SfTreeView without using Model class in Bound mode. 
 
Please let us know if you need further assistance. 
 
Regards, 
Lakshmi Natarajan

Loader.
Up arrow icon