|
| 1 | +--- |
| 2 | +title: Adjusting TreeView Row Height and Expand Button Margin |
| 3 | +description: Learn how to set the row height and adjust the margin of the expand button in the TreeView for UI for .NET MAUI. |
| 4 | +type: how-to |
| 5 | +page_title: Setting Row Height and Customizing Expand Button Margin in TreeView |
| 6 | +meta_title: Setting Row Height and Customizing Expand Button Margin in TreeView |
| 7 | +slug: adjusting-treeview-row-height-expand-button-margin |
| 8 | +tags: treeview, .net maui, row height, expand button, styling |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Version | Product | Author | |
| 15 | +| --- | --- | ---- | |
| 16 | +| 11.1.0 | Telerik UI for .NET MAUI TreeView | [Dobrinka Yordanova](https://www.telerik.com/blogs/author/dobrinka-yordanova) | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +I want to limit the row height in the [TreeView](https://www.telerik.com/maui-ui/documentation/controls/treeview/overview) and adjust the margin of the expand button. |
| 21 | + |
| 22 | +This knowledge base article also answers the following questions: |
| 23 | +- How to set a definitive row height in TreeView? |
| 24 | +- How to customize the margin of the expand button in TreeView? |
| 25 | +- How to apply styles to TreeView items and buttons? |
| 26 | + |
| 27 | +## Solution |
| 28 | + |
| 29 | +To set a definitive height for rows and adjust the margin of the expand button in TreeView, apply custom styles using `ItemStyle`. Follow these steps: |
| 30 | + |
| 31 | +1. Define a style for the TreeView item button (`TreeViewItemButton_Style`) to customize properties such as margin, padding, height, and width. |
| 32 | + |
| 33 | +```xaml |
| 34 | +<Style x:Key="TreeViewItemButton_Style" |
| 35 | + TargetType="telerik:TreeViewItemButton"> |
| 36 | + <Setter Property="BackgroundColor" Value="Transparent" /> |
| 37 | + <Setter Property="Margin" Value="5" /> |
| 38 | + <Setter Property="WidthRequest" Value="{OnPlatform Android=48, iOS=44, MacCatalyst=24, WinUI=26}" /> |
| 39 | + <Setter Property="HeightRequest" Value="{OnPlatform Android=48, iOS=44, MacCatalyst=24, WinUI=26}" /> |
| 40 | +</Style> |
| 41 | +``` |
| 42 | + |
| 43 | +2. Create a style for the expand button (`TreeViewItemExpandButton_Style`) based on the button style. Customize the appearance of the expand button. |
| 44 | + |
| 45 | +```xaml |
| 46 | +<Style x:Key="TreeViewItemExpandButton_Style" |
| 47 | + TargetType="telerik:TreeViewItemExpandButton" |
| 48 | + BasedOn="{StaticResource TreeViewItemButton_Style}"> |
| 49 | + <Setter Property="Text" Value="{x:Static telerik:TelerikFont.IconEmpty}" /> |
| 50 | + <Setter Property="VisualStateManager.VisualStateGroups"> |
| 51 | + <VisualStateGroupList> |
| 52 | + <VisualStateGroup Name="ExpandStates"> |
| 53 | + <VisualState Name="Expanded"> |
| 54 | + <VisualState.Setters> |
| 55 | + <Setter Property="Text" Value="{x:Static telerik:TelerikFont.IconDownArrow}" /> |
| 56 | + </VisualState.Setters> |
| 57 | + </VisualState> |
| 58 | + <VisualState Name="Collapsed"> |
| 59 | + <VisualState.Setters> |
| 60 | + <Setter Property="Text" Value="{x:Static telerik:TelerikFont.IconRightArrow}" /> |
| 61 | + </VisualState.Setters> |
| 62 | + </VisualState> |
| 63 | + </VisualStateGroup> |
| 64 | + </VisualStateGroupList> |
| 65 | + </Setter> |
| 66 | +</Style> |
| 67 | +``` |
| 68 | + |
| 69 | +3. Define a style for the TreeView item view (`TreeViewStyle`) to set row height and apply the custom expand button style. |
| 70 | + |
| 71 | +```xaml |
| 72 | +<Style x:Key="TreeViewStyle" TargetType="telerik:TreeViewItemView"> |
| 73 | + <Setter Property="Margin" Value="10,0" /> |
| 74 | + <Setter Property="HeightRequest" Value="60" /> |
| 75 | + <Setter Property="ExpandButtonStyle" Value="{StaticResource TreeViewItemExpandButton_Style}" /> |
| 76 | +</Style> |
| 77 | +``` |
| 78 | + |
| 79 | +4. Apply the styles to the RadTreeView definition. |
| 80 | + |
| 81 | +```xaml |
| 82 | +<telerik:RadTreeView x:Name="treeView" Grid.Row="1" |
| 83 | + AutomationId="treeView" ItemStyle="{StaticResource TreeViewStyle}" |
| 84 | + ItemsSource="{Binding TreeData}"> |
| 85 | + <telerik:TreeViewDescriptor DisplayMemberPath="Name" |
| 86 | + ItemsSourcePath="Children" |
| 87 | + TargetType="{x:Type local:TreeDataItem}" /> |
| 88 | + <telerik:RadTreeView.BindingContext> |
| 89 | + <local:MainPageViewModel/> |
| 90 | + </telerik:RadTreeView.BindingContext> |
| 91 | +</telerik:RadTreeView> |
| 92 | +``` |
| 93 | + |
| 94 | +## See Also |
| 95 | + |
| 96 | +- [TreeView Overview](https://www.telerik.com/maui-ui/documentation/controls/treeview/overview) |
| 97 | +- [TreeView Styling Documentation](https://www.telerik.com/maui-ui/documentation/controls/treeview/styling/item-style) |
0 commit comments