WIP
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.Xaml.Interactivity;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class AttachedEventTriggerBehaviour : Trigger
|
||||
{
|
||||
public static readonly StyledProperty<RoutedEvent> RoutedEventProperty =
|
||||
AvaloniaProperty.Register<AttachedEventTriggerBehaviour, RoutedEvent>(nameof(RoutedEvent));
|
||||
|
||||
public RoutedEvent RoutedEvent
|
||||
{
|
||||
get => GetValue(RoutedEventProperty);
|
||||
set => SetValue(RoutedEventProperty, value);
|
||||
}
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
if (RoutedEvent is not null)
|
||||
{
|
||||
if (AssociatedObject is Interactive interactive)
|
||||
{
|
||||
interactive.AddHandler(RoutedEvent, (object sender, RoutedEventArgs args) => {
|
||||
|
||||
Interaction.ExecuteActions(AssociatedObject, Actions, null);
|
||||
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
base.OnAttached();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class ItemInvokedEventArgs :
|
||||
RoutedEventArgs
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Interactivity;
|
||||
using Avalonia.LogicalTree;
|
||||
using Toolkit.UI.Controls.Avalonia;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class NavigationViewItemExtension
|
||||
{
|
||||
public static readonly AttachedProperty<bool> IsItemClickEnabledProperty =
|
||||
AvaloniaProperty.RegisterAttached<NavigationViewItem, bool>("IsItemClickEnabled",
|
||||
typeof(NavigationViewItemExtension), false);
|
||||
|
||||
public static readonly RoutedEvent<ItemInvokedEventArgs> ItemClickEvent =
|
||||
RoutedEvent.Register<ItemInvokedEventArgs>("ItemClick",
|
||||
RoutingStrategies.Bubble, typeof(NavigationViewItemExtension));
|
||||
|
||||
static NavigationViewItemExtension()
|
||||
{
|
||||
IsItemClickEnabledProperty.Changed.AddClassHandler<NavigationViewItem>(OnIsItemClickEnabledPropertyChanged);
|
||||
}
|
||||
|
||||
private static void OnIsItemClickEnabledPropertyChanged(NavigationViewItem sender,
|
||||
AvaloniaPropertyChangedEventArgs args)
|
||||
{
|
||||
bool TrySetupNavigationView()
|
||||
{
|
||||
if (sender.GetLogicalAncestors().OfType<NavigationView>().FirstOrDefault() is NavigationView navigationView)
|
||||
{
|
||||
void OnItemInvoked(object? _, FluentAvalonia.UI.Controls.NavigationViewItemInvokedEventArgs args)
|
||||
{
|
||||
if (args.InvokedItemContainer == sender)
|
||||
{
|
||||
sender.RaiseEvent(new ItemInvokedEventArgs { RoutedEvent = ItemClickEvent });
|
||||
}
|
||||
}
|
||||
|
||||
navigationView.ItemInvoked += OnItemInvoked;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!TrySetupNavigationView())
|
||||
{
|
||||
void OnAttachedToVisualTree(object? _, VisualTreeAttachmentEventArgs __)
|
||||
{
|
||||
sender.AttachedToVisualTree -= OnAttachedToVisualTree;
|
||||
TrySetupNavigationView();
|
||||
}
|
||||
|
||||
sender.AttachedToVisualTree += OnAttachedToVisualTree;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool GetIsItemClickEnabled(NavigationViewItem element) =>
|
||||
element.GetValue(IsItemClickEnabledProperty);
|
||||
|
||||
public static void SetIsItemClickEnabled(NavigationViewItem element, bool value) =>
|
||||
element.SetValue(IsItemClickEnabledProperty, value);
|
||||
|
||||
public static void AddItemClickHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.AddHandler(ItemClickEvent, handler);
|
||||
|
||||
public static void RemoveItemClickHandler(NavigationViewItem element, EventHandler<ItemInvokedEventArgs> handler) =>
|
||||
element.RemoveHandler(ItemClickEvent, handler);
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
using Avalonia;
|
||||
using Avalonia.VisualTree;
|
||||
using Avalonia.Xaml.Interactivity;
|
||||
using FluentAvalonia.Core;
|
||||
using System.Collections;
|
||||
using Toolkit.UI.Controls.Avalonia;
|
||||
using ISelectable = Toolkit.Foundation.ISelectable;
|
||||
|
||||
namespace Toolkit.UI.Avalonia;
|
||||
|
||||
public class NavigationViewItemInvokedBehaviour : Trigger
|
||||
{
|
||||
public static readonly StyledProperty<bool> SelectsChildOnInvokedProperty =
|
||||
AvaloniaProperty.Register<NavigationViewItemInvokedBehaviour, bool>(nameof(SelectsChildOnInvoked));
|
||||
|
||||
private NavigationView? navigationView;
|
||||
|
||||
public event TypedEventHandler<NavigationViewItem, EventArgs>? Invoked;
|
||||
|
||||
public bool SelectsChildOnInvoked
|
||||
{
|
||||
get => GetValue(SelectsChildOnInvokedProperty);
|
||||
set => SetValue(SelectsChildOnInvokedProperty, value);
|
||||
}
|
||||
|
||||
protected override void OnAttached()
|
||||
{
|
||||
if (AssociatedObject is NavigationViewItem navigationViewItem)
|
||||
{
|
||||
navigationViewItem.AttachedToVisualTree += OnAttachedToVisualTree;
|
||||
}
|
||||
|
||||
base.OnAttached();
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromVisualTree()
|
||||
{
|
||||
if (navigationView is not null)
|
||||
{
|
||||
navigationView.ItemInvoked -= OnItemInvoked;
|
||||
}
|
||||
|
||||
base.OnDetachedFromVisualTree();
|
||||
}
|
||||
|
||||
private void OnAttachedToVisualTree(object? sender,
|
||||
VisualTreeAttachmentEventArgs args)
|
||||
{
|
||||
if (AssociatedObject is NavigationViewItem navigationViewItem)
|
||||
{
|
||||
navigationViewItem.AttachedToVisualTree -= OnAttachedToVisualTree;
|
||||
SetupNavigationView();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnItemInvoked(object? sender,
|
||||
FluentAvalonia.UI.Controls.NavigationViewItemInvokedEventArgs args)
|
||||
{
|
||||
if (AssociatedObject is NavigationViewItem navigationViewItem)
|
||||
{
|
||||
if (args.InvokedItemContainer == AssociatedObject)
|
||||
{
|
||||
Interaction.ExecuteActions(AssociatedObject, Actions, null);
|
||||
if (!navigationViewItem.IsChildSelected && navigationViewItem.MenuItemsSource is IList collection)
|
||||
{
|
||||
if (collection is { Count: > 0 } && collection[0] is ISelectable selectable)
|
||||
{
|
||||
selectable.Selected = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetupNavigationView()
|
||||
{
|
||||
if (AssociatedObject is NavigationViewItem navigationViewItem)
|
||||
{
|
||||
if (navigationViewItem.GetVisualAncestors().OfType<NavigationView>().FirstOrDefault() is NavigationView navigationView)
|
||||
{
|
||||
this.navigationView = navigationView;
|
||||
navigationView.ItemInvoked += OnItemInvoked;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user