diff --git a/src/Avalonia.Controls/TreeViewItem.cs b/src/Avalonia.Controls/TreeViewItem.cs index 33b0e54868..c5d3313117 100644 --- a/src/Avalonia.Controls/TreeViewItem.cs +++ b/src/Avalonia.Controls/TreeViewItem.cs @@ -6,7 +6,6 @@ using Avalonia.Controls.Generators; using Avalonia.Controls.Mixins; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; -using Avalonia.Data; using Avalonia.Input; using Avalonia.LogicalTree; @@ -37,8 +36,7 @@ namespace Avalonia.Controls /// public static readonly DirectProperty DepthProperty = AvaloniaProperty.RegisterDirect( - nameof(Depth), - o => o.Depth); + nameof(Depth), o => o.Depth); private static readonly ITemplate DefaultPanel = new FuncTemplate(() => new StackPanel()); @@ -80,27 +78,8 @@ namespace Avalonia.Controls /// public int Depth { - get { return this.GetDepth(this); } - } - - private int GetDepth(TreeViewItem item) - { - TreeViewItem parent; - while ((parent = GetParent(item)) != null) - { - return GetDepth(parent) + 1; - } - return 0; - } - - private static TreeViewItem GetParent(TreeViewItem item) - { - var parent = item.InheritanceParent; - while (!(parent == null || parent is TreeViewItem || parent is TreeView)) - { - parent = item.Parent; - } - return parent as TreeViewItem; + get { return _depth; } + private set { SetAndRaise(DepthProperty, ref _depth, value); } } /// @@ -127,6 +106,8 @@ namespace Avalonia.Controls base.OnAttachedToLogicalTree(e); _treeView = this.GetLogicalAncestors().OfType().FirstOrDefault(); + Depth = this.CalculateDistanceFromLogicalParent() - 1; + if (ItemTemplate == null && _treeView?.ItemTemplate != null) { ItemTemplate = _treeView.ItemTemplate; diff --git a/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs b/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs index 276ea6c060..47550a876d 100644 --- a/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs +++ b/src/Avalonia.Styling/LogicalTree/LogicalExtensions.cs @@ -87,5 +87,20 @@ namespace Avalonia.LogicalTree { return target.GetLogicalAncestors().Any(x => x == logical); } + + public static int CalculateDistanceFromLogicalParent(this ILogical logical, int @default = -1) where T : class + { + Contract.Requires(logical != null); + + var result = 0; + + while (logical != null && logical.GetType() != typeof(T)) + { + ++result; + logical = logical.LogicalParent; + } + + return logical != null ? result : @default; + } } } diff --git a/src/Avalonia.Themes.Default/TreeViewItem.xaml b/src/Avalonia.Themes.Default/TreeViewItem.xaml index a51b6c0aca..5c5012e685 100644 --- a/src/Avalonia.Themes.Default/TreeViewItem.xaml +++ b/src/Avalonia.Themes.Default/TreeViewItem.xaml @@ -1,91 +1,92 @@ - + xmlns:converters="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls"> + - + - - - + - + - + - + - - - + - + + + + +