|
|
@@ -21,7 +21,7 @@ namespace Avalonia.Controls
|
|
|
/// </summary>
|
|
|
public static readonly DirectProperty<TreeViewItem, bool> IsExpandedProperty =
|
|
|
AvaloniaProperty.RegisterDirect<TreeViewItem, bool>(
|
|
|
- "IsExpanded",
|
|
|
+ nameof(IsExpanded),
|
|
|
o => o.IsExpanded,
|
|
|
(o, v) => o.IsExpanded = v);
|
|
|
|
|
|
@@ -31,17 +31,25 @@ namespace Avalonia.Controls
|
|
|
public static readonly StyledProperty<bool> IsSelectedProperty =
|
|
|
ListBoxItem.IsSelectedProperty.AddOwner<TreeViewItem>();
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Defines the <see cref="Level"/> property.
|
|
|
+ /// </summary>
|
|
|
+ public static readonly DirectProperty<TreeViewItem, int> LevelProperty =
|
|
|
+ AvaloniaProperty.RegisterDirect<TreeViewItem, int>(
|
|
|
+ nameof(Level), o => o.Level);
|
|
|
+
|
|
|
private static readonly ITemplate<IPanel> DefaultPanel =
|
|
|
new FuncTemplate<IPanel>(() => new StackPanel());
|
|
|
|
|
|
private TreeView _treeView;
|
|
|
private bool _isExpanded;
|
|
|
+ private int _level;
|
|
|
|
|
|
/// <summary>
|
|
|
/// Initializes static members of the <see cref="TreeViewItem"/> class.
|
|
|
/// </summary>
|
|
|
static TreeViewItem()
|
|
|
- {
|
|
|
+ {
|
|
|
SelectableMixin.Attach<TreeViewItem>(IsSelectedProperty);
|
|
|
FocusableProperty.OverrideDefaultValue<TreeViewItem>(true);
|
|
|
ItemsPanelProperty.OverrideDefaultValue<TreeViewItem>(DefaultPanel);
|
|
|
@@ -65,6 +73,15 @@ namespace Avalonia.Controls
|
|
|
set { SetValue(IsSelectedProperty, value); }
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Gets the level/indentation of the item.
|
|
|
+ /// </summary>
|
|
|
+ public int Level
|
|
|
+ {
|
|
|
+ get { return _level; }
|
|
|
+ private set { SetAndRaise(LevelProperty, ref _level, value); }
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// Gets the <see cref="ITreeItemContainerGenerator"/> for the tree view.
|
|
|
/// </summary>
|
|
|
@@ -89,6 +106,8 @@ namespace Avalonia.Controls
|
|
|
base.OnAttachedToLogicalTree(e);
|
|
|
_treeView = this.GetLogicalAncestors().OfType<TreeView>().FirstOrDefault();
|
|
|
|
|
|
+ Level = CalculateDistanceFromLogicalParent<TreeView>(this) - 1;
|
|
|
+
|
|
|
if (ItemTemplate == null && _treeView?.ItemTemplate != null)
|
|
|
{
|
|
|
ItemTemplate = _treeView.ItemTemplate;
|
|
|
@@ -126,5 +145,18 @@ namespace Avalonia.Controls
|
|
|
|
|
|
// Don't call base.OnKeyDown - let events bubble up to containing TreeView.
|
|
|
}
|
|
|
+
|
|
|
+ private static int CalculateDistanceFromLogicalParent<T>(ILogical logical, int @default = -1) where T : class
|
|
|
+ {
|
|
|
+ var result = 0;
|
|
|
+
|
|
|
+ while (logical != null && logical.GetType() != typeof(T))
|
|
|
+ {
|
|
|
+ ++result;
|
|
|
+ logical = logical.LogicalParent;
|
|
|
+ }
|
|
|
+
|
|
|
+ return logical != null ? result : @default;
|
|
|
+ }
|
|
|
}
|
|
|
}
|