Skip to content

Commit

Permalink
chore: add avalonia TreeView demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Feb 22, 2025
1 parent 2c05bce commit eca5885
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="HandyControlDemo.UserControl.TreeViewDemo">
<WrapPanel Margin="16">
<TreeView Width="200"
Margin="16"
ItemsSource="{Binding DataList}">
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding DataList}">
<TextBlock Text="{Binding Name}" />
</TreeDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
<TreeView Width="200"
Margin="16"
ItemsSource="{Binding DataList}"
Theme="{StaticResource TreeView.Small}">
<TreeView.ItemTemplate>
<TreeDataTemplate ItemsSource="{Binding DataList}">
<TextBlock Text="{Binding Name}" />
</TreeDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</WrapPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace HandyControlDemo.UserControl;

public partial class TreeViewDemo : Avalonia.Controls.UserControl
{
public TreeViewDemo()
{
InitializeComponent();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
<hc:CornerRadiusSplitConverter x:Key="CornerRadiusSplitConverter" />
<hc:DoubleExpandConverter x:Key="DoubleExpandConverter" />
<avalonia:StringFormatConverter x:Key="StringFormatConverter" />
<avalonia:MarginMultiplierConverter x:Key="LeftMarginConverter"
Indent="16"
Left="True" />

</ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="CornerRadius"
Value="{StaticResource DefaultCornerRadius}" />
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
<Setter Property="Padding"
Value="2,2,2,0" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
Expand All @@ -76,8 +76,7 @@
Value="{StaticResource DefaultCornerRadius}" />
<Setter Property="Template">
<ControlTemplate>
<Border Name="border"
BorderBrush="{TemplateBinding BorderBrush}"
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
Expand Down
187 changes: 187 additions & 0 deletions src/Avalonia/HandyControl_Avalonia/Themes/Styles/TreeView.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:hc="clr-namespace:HandyControl.Controls">
<Geometry x:Key="TreeArrow">M508.893248 762.931659a65.039498 65.039498 0 0 1-46.420659-19.485839L97.125746 371.004022c-25.143018-25.634319-24.752868-66.816849 0.881451-91.959868 25.648769-25.164693 66.809624-24.745643 91.959867 0.881451l318.933409 325.125238 318.933408-325.125238a65.025048 65.025048 0 0 1 92.841318 91.078417L555.313907 743.44582a65.025048 65.025048 0 0 1-46.420659 19.485839z</Geometry>

<ControlTheme x:Key="ExpandCollapseToggleStyle"
TargetType="ToggleButton">
<Setter Property="Width"
Value="16" />
<Setter Property="Height"
Value="16" />
<Setter Property="Template">
<ControlTemplate>
<Border Background="Transparent"
Height="16"
Padding="4"
Width="16">
<Path x:Name="expandPath"
Stretch="Uniform"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Data="{StaticResource TreeArrow}"
Fill="{TemplateBinding Foreground}">
<Path.RenderTransform>
<RotateTransform Angle="-90" />
</Path.RenderTransform>
</Path>
</Border>
</ControlTemplate>
</Setter>

<Style Selector="^:checked /template/ Path#expandPath">
<Setter Property="RenderTransform">
<RotateTransform Angle="0" />
</Setter>
</Style>
</ControlTheme>

<ControlTheme x:Key="TreeViewItemBaseStyle"
TargetType="TreeViewItem">
<Setter Property="Padding"
Value="10,0" />
<Setter Property="MinHeight"
Value="{StaticResource DefaultControlHeight}" />
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="BorderBrush"
Value="Transparent" />
<Setter Property="CornerRadius"
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
<Setter Property="BorderThickness"
Value="0" />
<Setter Property="Margin"
Value="0,0,0,2" />
<Setter Property="Template">
<ControlTemplate>
<StackPanel>
<Border Name="SelectionBorder"
MinHeight="{TemplateBinding MinHeight}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
Focusable="True"
Padding="{TemplateBinding Padding}"
TemplatedControl.IsTemplateFocusTarget="True">
<Grid Name="PART_Header"
Margin="{TemplateBinding Level, Mode=OneWay, Converter={StaticResource LeftMarginConverter}}"
ColumnDefinitions="16, *">
<ToggleButton Name="PART_ExpandCollapseChevron"
Focusable="False"
Background="Transparent"
IsChecked="{TemplateBinding IsExpanded, Mode=TwoWay}"
Theme="{StaticResource ExpandCollapseToggleStyle}" />
<ContentPresenter Name="PART_HeaderPresenter"
Grid.Column="1"
Background="Transparent"
VerticalAlignment="Center"
HorizontalContentAlignment="{TemplateBinding HorizontalAlignment}"
Content="{TemplateBinding Header}"
ContentTemplate="{TemplateBinding HeaderTemplate}"
Focusable="False" />
</Grid>
</Border>
<ItemsPresenter Name="PART_ItemsPresenter"
IsVisible="{TemplateBinding IsExpanded}"
ItemsPanel="{TemplateBinding ItemsPanel}" />
</StackPanel>
</ControlTemplate>
</Setter>

<Style Selector="^ /template/ Border#SelectionBorder:pointerover">
<Setter Property="Background"
Value="{DynamicResource SecondaryRegionBrush}" />
</Style>

<Style Selector="^:selected">
<Style Selector="^ /template/ Border#SelectionBorder, ^ /template/ Border#SelectionBorder:pointerover">
<Setter Property="Background"
Value="{DynamicResource PrimaryBrush}" />
</Style>
<Style Selector="^ /template/ ContentPresenter#PART_HeaderPresenter">
<Setter Property="Foreground"
Value="{DynamicResource TextIconBrush}" />
</Style>
<Style Selector="^ /template/ ToggleButton#PART_ExpandCollapseChevron">
<Setter Property="Foreground"
Value="{DynamicResource TextIconBrush}" />
</Style>
</Style>

<Style Selector="^:empty /template/ ToggleButton#PART_ExpandCollapseChevron">
<Setter Property="IsVisible"
Value="False" />
</Style>

<Style Selector="^:disabled">
<Setter Property="Opacity"
Value="0.4" />
</Style>
</ControlTheme>

<ControlTheme x:Key="TreeViewBaseStyle"
TargetType="TreeView">
<Setter Property="Background"
Value="{DynamicResource RegionBrush}" />
<Setter Property="BorderBrush"
Value="{DynamicResource BorderBrush}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="CornerRadius"
Value="{Binding $self.(hc:BorderElement.CornerRadius)}" />
<Setter Property="Padding"
Value="2,2,2,0" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility"
Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility"
Value="Auto" />
<Setter Property="ScrollViewer.IsScrollChainingEnabled"
Value="True" />
<Setter Property="ItemContainerTheme"
Value="{StaticResource TreeViewItemBaseStyle}" />
<Setter Property="hc:BorderElement.CornerRadius"
Value="{StaticResource DefaultCornerRadius}" />
<Setter Property="Template">
<ControlTemplate>
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
CornerRadius="{TemplateBinding CornerRadius}">
<ScrollViewer AllowAutoHide="{TemplateBinding (ScrollViewer.AllowAutoHide)}"
BringIntoViewOnFocusChange="{TemplateBinding (ScrollViewer.BringIntoViewOnFocusChange)}"
HorizontalScrollBarVisibility="{TemplateBinding (ScrollViewer.HorizontalScrollBarVisibility)}"
IsScrollChainingEnabled="{TemplateBinding (ScrollViewer.IsScrollChainingEnabled)}"
IsDeferredScrollingEnabled="{TemplateBinding (ScrollViewer.IsDeferredScrollingEnabled)}"
VerticalScrollBarVisibility="{TemplateBinding (ScrollViewer.VerticalScrollBarVisibility)}"
VerticalSnapPointsType="{TemplateBinding (ScrollViewer.VerticalSnapPointsType)}"
HorizontalSnapPointsType="{TemplateBinding (ScrollViewer.HorizontalSnapPointsType)}">
<ItemsPresenter Name="PART_ItemsPresenter"
Margin="{TemplateBinding Padding}"
ItemsPanel="{TemplateBinding ItemsPanel}" />
</ScrollViewer>
</Border>
</ControlTemplate>
</Setter>
</ControlTheme>

<ControlTheme x:Key="{x:Type TreeView}"
BasedOn="{StaticResource TreeViewBaseStyle}"
TargetType="TreeView" />

<ControlTheme x:Key="TreeViewItemBaseStyle.Small"
BasedOn="{StaticResource TreeViewItemBaseStyle}"
TargetType="TreeViewItem">
<Setter Property="Padding"
Value="6,0" />
<Setter Property="MinHeight"
Value="24" />
</ControlTheme>

<ControlTheme x:Key="TreeView.Small"
BasedOn="{StaticResource TreeViewBaseStyle}"
TargetType="TreeView">
<Setter Property="ItemContainerTheme"
Value="{StaticResource TreeViewItemBaseStyle.Small}" />
</ControlTheme>
</ResourceDictionary>
1 change: 1 addition & 0 deletions src/Avalonia/HandyControl_Avalonia/Themes/Theme.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<MergeResourceInclude Source="/Themes/Styles/TabControl.axaml" />
<MergeResourceInclude Source="/Themes/Styles/ScrollViewer.axaml" />
<MergeResourceInclude Source="/Themes/Styles/ListBox.axaml" />
<MergeResourceInclude Source="/Themes/Styles/TreeView.axaml" />
<MergeResourceInclude Source="/Themes/Styles/Label.axaml" />
<MergeResourceInclude Source="/Themes/Styles/Slider.axaml" />
<MergeResourceInclude Source="/Themes/Styles/Expander.axaml" />
Expand Down

0 comments on commit eca5885

Please sign in to comment.