|
| 1 | +using Avalonia.Controls; |
| 2 | +using Avalonia.Controls.Primitives; |
| 3 | +using Avalonia.Controls.Templates; |
| 4 | +using Avalonia.Data; |
| 5 | +using Avalonia.Markup.Xaml.Templates; |
| 6 | +using Lemon.ModuleNavigation.Abstractions; |
| 7 | +using System.ComponentModel; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | + |
| 10 | +namespace Lemon.ModuleNavigation.Avaloniaui.Regions; |
| 11 | + |
| 12 | +public class ItemsRegion : Region, IItemsRegionDataContext<IDataTemplate> |
| 13 | +{ |
| 14 | + private readonly ItemsControl _itemsControl; |
| 15 | + public ItemsRegion(string name, ItemsControl itemsControl) : base(name) |
| 16 | + { |
| 17 | + _itemsControl = itemsControl; |
| 18 | + SetBindingItemTemplate(); |
| 19 | + SetBindingSelectedItem(); |
| 20 | + SetBindingItemsSource(); |
| 21 | + } |
| 22 | + private object? _selectItem; |
| 23 | + public object? SelectedItem |
| 24 | + { |
| 25 | + get |
| 26 | + { |
| 27 | + return _selectItem; |
| 28 | + } |
| 29 | + set |
| 30 | + { |
| 31 | + _selectItem = value; |
| 32 | + OnPropertyChanged(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private IDataTemplate? _itemsTemplate; |
| 37 | + public IDataTemplate? ItemTemplate |
| 38 | + { |
| 39 | + get => _itemsTemplate; |
| 40 | + set |
| 41 | + { |
| 42 | + _itemsTemplate = value; |
| 43 | + OnPropertyChanged(); |
| 44 | + } |
| 45 | + } |
| 46 | + public event PropertyChangedEventHandler? PropertyChanged; |
| 47 | + |
| 48 | + public override void ScrollIntoView(int index) |
| 49 | + { |
| 50 | + _itemsControl.ScrollIntoView(index); |
| 51 | + } |
| 52 | + public override void ScrollIntoView(NavigationContext item) |
| 53 | + { |
| 54 | + _itemsControl.ScrollIntoView(item); |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// When Views with same ViewName were found, the earliest one will be picked. |
| 59 | + /// </summary> |
| 60 | + /// <param name="target"></param> |
| 61 | + public override void Activate(NavigationContext target) |
| 62 | + { |
| 63 | + try |
| 64 | + { |
| 65 | + if (ViewCache.TryGetValue(target, out IView? accurateView)) |
| 66 | + { |
| 67 | + target.View = accurateView; |
| 68 | + SelectedItem = target; |
| 69 | + return; |
| 70 | + } |
| 71 | + var context = Contexts.FirstOrDefault(c => c.ViewName == target.ViewName); |
| 72 | + if (context is not null |
| 73 | + && context.View is not null |
| 74 | + && context.View.DataContext is INavigationAware navigationAware |
| 75 | + && navigationAware.IsNavigationTarget(target)) |
| 76 | + { |
| 77 | + SelectedItem = context; |
| 78 | + return; |
| 79 | + } |
| 80 | + Contexts.Add(target); |
| 81 | + SelectedItem = target; |
| 82 | + } |
| 83 | + finally |
| 84 | + { |
| 85 | + ScrollIntoView((SelectedItem as NavigationContext)!); |
| 86 | + } |
| 87 | + } |
| 88 | + public override void DeActivate(string viewName) |
| 89 | + { |
| 90 | + Contexts.Remove(Contexts.Last(c => c.ViewName == viewName)); |
| 91 | + } |
| 92 | + public override void DeActivate(NavigationContext navigationContext) |
| 93 | + { |
| 94 | + Contexts.Remove(navigationContext); |
| 95 | + } |
| 96 | + public void Add(NavigationContext item) |
| 97 | + { |
| 98 | + Contexts.Add(item); |
| 99 | + } |
| 100 | + |
| 101 | + protected virtual void SetBindingItemsSource() |
| 102 | + { |
| 103 | + _itemsControl.Bind(ItemsControl.ItemsSourceProperty, |
| 104 | + new Binding(nameof(Contexts)) |
| 105 | + { |
| 106 | + Source = this |
| 107 | + }); |
| 108 | + } |
| 109 | + protected virtual void SetBindingItemTemplate() |
| 110 | + { |
| 111 | + ItemTemplate = RegionContentTemplate; |
| 112 | + _itemsControl.Bind(ItemsControl.ItemTemplateProperty, |
| 113 | + new Binding(nameof(ItemTemplate)) |
| 114 | + { |
| 115 | + Source = this |
| 116 | + }); |
| 117 | + } |
| 118 | + protected virtual void SetBindingSelectedItem() |
| 119 | + { |
| 120 | + if (_itemsControl is SelectingItemsControl selector) |
| 121 | + { |
| 122 | + selector.Bind(SelectingItemsControl.SelectedItemProperty, |
| 123 | + new Binding(nameof(SelectedItem)) |
| 124 | + { |
| 125 | + Source = this, |
| 126 | + Mode = BindingMode.TwoWay |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + protected void OnPropertyChanged([CallerMemberName] string? propertyName = null) |
| 131 | + { |
| 132 | + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); |
| 133 | + } |
| 134 | +} |
0 commit comments