How to filter data source to treeview?

How i filter data source to tree view, and i need only checkbox to be visible only in leaf children node not in parent node?

Here i am attaching the json signature to be binded in treeview itemsource(but after filtering based on subdisease id) and a screenshot where parent node has no checkbox and leaf child node should have checkbox.

Attachment: Syncfusion_Treeview_7b93bf3f.rar

1 Reply

LN Lakshmi Natarajan Syncfusion Team February 24, 2020 12:56 PM UTC

Hi Richy, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query “how to filter data source to treeview” from our end. 
 
#Regarding CheckBox query 
 
The SfTreeView allows you to customize the ItemTemplate based on Level. We would like to let you know that you can use converter to handle the visibility for CheckBox based on the Level. Please refer the following code snippet for more reference, 
 
Xaml:  
 
  <TreeView:SfTreeView.ItemTemplate> 
                <DataTemplate> 
                    <Grid> 
                        <Grid.ColumnDefinitions> 
                            <ColumnDefinition Width="{Binding Level, Converter={StaticResource CheckBoxWidthConverter}}"/> 
                            <ColumnDefinition Width="*"/> 
                        </Grid.ColumnDefinitions> 
                        <SfButtons:SfCheckBox  
                            x:Name="CheckBox" Margin="10, 0, 0, 0" 
                            IsVisible="{Binding Level, Converter={StaticResource CheckBoxVisibilityConverter}}" 
                            IsChecked="{Binding IsChecked, Mode=TwoWay}"/> 
                        <Label Text="{Binding Content.FileName}" Grid.Column="1"/> 
                    </Grid> 
                </DataTemplate> 
            </TreeView:SfTreeView.ItemTemplate> 
 
Converter: 
 
  public class CheckBoxVisibilityConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            return (int)value == 0 ? false : true; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    } 
 
    public class CheckBoxWidthConverter : IValueConverter 
    { 
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            return (int)value == 0 ? 0 : 50; 
        } 
 
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    } 
 
 
#Regarding Filtering SfTreeView 
 
You can filter the SfTreeView items in sample level using SearchButtonPressed event of SearchBar. Please refer the following code snippet for more reference, 
 
Xaml: 
 
   <SearchBar x:Name="searchBar"  
            Placeholder="Search Tree"  
                   HeightRequest="50"/> 
 
C#: 
 
       private void SearchBar_SearchButtonPressed(object sender, EventArgs e) 
        { 
            if (SearchBar.Text == "") 
            { 
                TreeView.ItemsSource = viewModel.Folders; 
                return; 
            } 
 
            var searchText = SearchBar.Text; 
 
            foreach (var node in viewModel.Folders) 
            { 
                if (node.FileName.ToLower().Contains(searchText.ToLower())) 
                { 
                    filterViewModel.Clear(); 
                    filterViewModel.Add(node); 
                } 
                else 
                { 
                    foreach (var child in node.Files) 
                    { 
                        if (child.FileName.ToLower().Contains(searchText.ToLower())) 
                        { 
                            filterViewModel.Clear(); 
                            filterViewModel.Add(child); 
                        } 
                    } 
                } 
            } 
 
            TreeView.ItemsSource = filterViewModel; 
        } 
 
 
 
We have prepared a simple sample based on your requirement. Please find the sample in the following link, 
 
 
Please check the sample and let us know if you need any further assistance. 
 
Regards, 
Lakshmi Natarajan 


Loader.
Up arrow icon