using System; using System.Collections.Generic; using System.Collections.ObjectModel; using Xamarin.Forms; namespace Aurora.Design.Components.NavigationMenu { public partial class NavigationMenu : ContentPage { public NavigationMenu() { InitializeComponent(); ListView = MenuItemsListView; } public ListView ListView; public static readonly BindableProperty ItemsProperty = BindableProperty.Create(propertyName: nameof(Items), returnType: typeof(ObservableCollection), declaringType: typeof(NavigationMenu), defaultBindingMode: BindingMode.TwoWay, propertyChanged: OnItemsChanged); public ObservableCollection Items { get { return (ObservableCollection)GetValue(ItemsProperty); } set { SetValue(ItemsProperty, value); } } /// /// Items changed event handler. Organizes items in groups for display. /// /// The changed Item. /// The previous value. /// The new value. private static void OnItemsChanged(BindableObject bindable, object oldValue, object newValue) { var control = (NavigationMenu)bindable; ObservableCollection items = (ObservableCollection)newValue; Dictionary groupDictioanry = new Dictionary(); //Populate dictionary where group heading is the key foreach (NavigationItem item in items) { if (groupDictioanry.ContainsKey(item.Group)) { groupDictioanry.TryGetValue(item.Group, out var groupItem); groupItem.Items.Add(item); } else { NavigationGroupItem groupItem = new NavigationGroupItem(item.Group); groupItem.Add(item); groupDictioanry.Add(item.Group, groupItem); } } ObservableCollection groups = new ObservableCollection(); foreach (string groupHeading in groupDictioanry.Keys) { groupDictioanry.TryGetValue(groupHeading, out var groupItem); groups.Add(groupItem); } control.MenuItemsListView.ItemsSource = groups; } } }