feat: try to setup communication.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.LogicalTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -14,4 +15,18 @@ public class NavMenu: SelectingItemsControl
|
||||
{
|
||||
return new NavMenuItem();
|
||||
}
|
||||
|
||||
internal void SelectItem(NavMenuItem item)
|
||||
{
|
||||
if (item.IsSelected) return;
|
||||
var children = this.LogicalChildren.OfType<NavMenuItem>();
|
||||
foreach (var child in children)
|
||||
{
|
||||
if (child != item)
|
||||
{
|
||||
child.IsSelected = false;
|
||||
}
|
||||
}
|
||||
item.IsSelected = true;
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Mixins;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Controls.Templates;
|
||||
using Avalonia.Input;
|
||||
using Avalonia.LogicalTree;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class NavMenuItem: HeaderedSelectingItemsControl
|
||||
{
|
||||
private NavMenu? _rootMenu;
|
||||
|
||||
public static readonly StyledProperty<object?> IconProperty = AvaloniaProperty.Register<NavMenuItem, object?>(
|
||||
nameof(Icon));
|
||||
|
||||
@@ -14,6 +21,30 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
||||
get => GetValue(IconProperty);
|
||||
set => SetValue(IconProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDataTemplate?> IconTemplateProperty = AvaloniaProperty.Register<NavMenuItem, IDataTemplate?>(
|
||||
nameof(IconTemplate));
|
||||
|
||||
public IDataTemplate? IconTemplate
|
||||
{
|
||||
get => GetValue(IconTemplateProperty);
|
||||
set => SetValue(IconTemplateProperty, value);
|
||||
}
|
||||
|
||||
public new static readonly StyledProperty<bool> IsSelectedProperty =
|
||||
SelectingItemsControl.IsSelectedProperty.AddOwner<NavMenuItem>();
|
||||
|
||||
public bool IsSelected
|
||||
{
|
||||
get => GetValue(IsSelectedProperty);
|
||||
set => SetValue(IsSelectedProperty, value);
|
||||
}
|
||||
|
||||
static NavMenuItem()
|
||||
{
|
||||
SelectableMixin.Attach<NavMenuItem>(IsSelectedProperty);
|
||||
PressedMixin.Attach<NavMenuItem>();
|
||||
}
|
||||
|
||||
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
|
||||
{
|
||||
@@ -24,4 +55,26 @@ public class NavMenuItem: HeaderedSelectingItemsControl
|
||||
{
|
||||
return new NavMenuItem();
|
||||
}
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
_rootMenu = GetRootMenu();
|
||||
}
|
||||
|
||||
protected override void OnPointerPressed(PointerPressedEventArgs e)
|
||||
{
|
||||
base.OnPointerPressed(e);
|
||||
_rootMenu?.SelectItem(this);
|
||||
}
|
||||
|
||||
private NavMenu? GetRootMenu()
|
||||
{
|
||||
var root = this.FindAncestorOfType<NavMenu>();
|
||||
if (root is null)
|
||||
{
|
||||
root = this.FindLogicalAncestorOfType<NavMenu>();
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user