Articles in this section
Category / Section

How to AutoFit the nodes based on the content in Xamarin.Forms TreeView (SfTreeView)

1 min read

You can auto fit the SfTreeView node height based on its content using QueryNodeSize in Xamarin.Forms.

C#

Setting node height based on its content using GetActualNodeHeight method.

namespace TreeViewXamarin
{
    public class Behavior : Behavior<SfTreeView>
    {
        protected override void OnAttachedTo(SfTreeView bindable)
        {
            bindable.QueryNodeSize += Bindable_QueryNodeSize;
            base.OnAttachedTo(bindable);
        }
 
        private void Bindable_QueryNodeSize(object sender, QueryNodeSizeEventArgs e)
        {
            // Returns item height based on the content loaded.
            e.Height = e.GetActualNodeHeight();
            e.Handled = true;
        }
        protected override void OnDetachingFrom(SfTreeView bindable)
        {
            bindable.QueryNodeSize -= Bindable_QueryNodeSize;
            base.OnDetachingFrom(bindable);
        }
    }
}

Output

TreeView QueryNodeSize

View sample in GitHub

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied