How to conditionally hide the checkbox in Xamarin.Forms TreeView (SfTreeView)?
You can show/hide the CheckBox based on Level in Xamarin.Forms TreeView.
XAML
Bind the SfCheckBox.IsVisible property with TreeViewNode.Level and define converter to handle the Visibility.
<treeView:SfTreeView x:Name="treeView" ItemTemplateContextType="Node" CheckBoxMode="Recursive" AutoExpandMode="RootNodesExpanded" CheckedItems="{Binding CheckedItems}" ItemsSource="{Binding Folders}"> <treeView:SfTreeView.HierarchyPropertyDescriptors> <treeViewEngine:HierarchyPropertyDescriptor TargetType="{x:Type local:Folder}" ChildPropertyName="SubFolder"/> <treeViewEngine:HierarchyPropertyDescriptor TargetType="{x:Type local:SubFolder}" ChildPropertyName="Files"/> <treeViewEngine:HierarchyPropertyDescriptor TargetType="{x:Type local:File}" ChildPropertyName="SubFiles"/> </treeView:SfTreeView.HierarchyPropertyDescriptors> <treeView:SfTreeView.ItemTemplate> <DataTemplate> <Grid Padding="5"> <Grid.ColumnDefinitions> <ColumnDefinition Width="40"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <checkBox:SfCheckBox x:Name="CheckBox" IsChecked="{Binding IsChecked, Mode=TwoWay}" CornerRadius="2" IsVisible="{Binding Level, Converter={StaticResource checkBoxVisibilityConverter}}"/> <Label Text="{Binding Content.FileName}" VerticalOptions="CenterAndExpand" HorizontalOptions="StartAndExpand" Grid.Column="1"/> </Grid> </DataTemplate> </treeView:SfTreeView.ItemTemplate> </treeView:SfTreeView>
C#
The converter returns the true/false, based on the Level.
public class CheckBoxVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { return (int)value == 0 ? false : true; } }
Conclusion
I hope you enjoyed learning about how to conditionally hide the checkbox in Xamarin.Forms TreeView (SfTreeView).
You can refer to our Xamarin.Forms TreeView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!