Skip to content

Latest commit

 

History

History
40 lines (36 loc) · 2.44 KB

File metadata and controls

40 lines (36 loc) · 2.44 KB

How to autofit the items based on the content in Xamarin Forms TreeView?

In TreeView, you can customize each item size by using the QueryNodeSize event. In this event, the Handled property indicates whether to set specified size for an item or not by its Boolean value. This event is raised when an item comes in view with the QueryNodeSizeEventArgs argument.

<ContentPage xmlns:syncfusion="clr-namespace:Syncfusion.XForms.TreeView;assembly=Syncfusion.SfTreeView.XForms"
             xmlns:local="clr-namespace:GettingStarted"
             x:Class="GettingStarted.MainPage">
    <ContentPage.BindingContext>
       <local:FileManagerViewModel x:Name="viewModel"></local:FileManagerViewModel>
    </ContentPage.BindingContext>
    <ContentPage.Content>
       <syncfusion:SfTreeView x:Name="treeView"
                              QueryNodeSize="TreeView_QueryNodeSize"
                              ChildPropertyName="SubFiles"
                              ItemsSource="{Binding ImageNodeInfo}"/>
       </syncfusion:SfTreeView>
    </ContentPage.Content>
</ContentPage>

The QueryNodeSizeEventArgs.GetActualNodeHeight method of QueryNodeSizeEventArgs is used to get the measured height of the item based on content and it is set to the QueryNodeSizeEventArgs.Height property.

private void TreeView_QueryNodeSize(object sender, QueryNodeSizeEventArgs e)
{
    if (e.Node.Level == 0)
    {
        //Returns speified item height for item.
        e.Height = 200;
        e.Handled = true;
    }
    else
    {
        // Returns measured height based on the content loaded.
        e.Height = e.GetActualNodeHeight();
        e.Handled = true;
    }
}