using static Blahblah.FlowerApp.Extensions; namespace Blahblah.FlowerApp.Controls; class ItemSelectorPage : ContentPage where T : IdTextItem { public EventHandler? Selected; public ItemSelectorPage(string title, T[] source, bool multiple = false, K[]? selected = null, string display = nameof(IdTextItem.Text), string? detail = null) { Title = title; var itemsSource = source.Select(t => new SelectableItem { Item = t, IsSelected = selected != null && selected.Contains(t.Id) }).ToArray(); var list = new ListView { SelectionMode = ListViewSelectionMode.None, ItemsSource = itemsSource, ItemTemplate = new DataTemplate(() => { var content = new Grid { Margin = new Thickness(12, 0), ColumnSpacing = 12, ColumnDefinitions = { new(30), new(GridLength.Star), new(GridLength.Auto) }, Children = { new SecondaryLabel { VerticalOptions = LayoutOptions.Center, HorizontalOptions = LayoutOptions.Center, Text = Res.Check, FontFamily = "FontAwesome" } .Binding(IsVisibleProperty, nameof(SelectableItem.IsSelected)), new Label { VerticalOptions = LayoutOptions.Center } .Binding(Label.TextProperty, $"{nameof(SelectableItem.Item)}.{display}") .GridColumn(1) } }; if (detail != null) { content.Children.Add( new SecondaryLabel { VerticalOptions = LayoutOptions.Center } .Binding(Label.TextProperty, $"{nameof(SelectableItem.Item)}.{detail}") .GridColumn(2)); } return new ViewCell { View = content }; }) }; list.ItemTapped += List_ItemTapped; Content = list; } private async void List_ItemTapped(object? sender, ItemTappedEventArgs e) { if (e.Item is SelectableItem item) { Selected?.Invoke(this, item.Item); await Navigation.PopAsync(); } } } class SelectableItem : BindableObject { public static BindableProperty IsSelectedProperty = CreateProperty>(nameof(IsSelected)); public static BindableProperty ItemProperty = CreateProperty>(nameof(Item)); public bool IsSelected { get => (bool)GetValue(IsSelectedProperty); set => SetValue(IsSelectedProperty, value); } public T Item { get => (T)GetValue(ItemProperty); set => SetValue(ItemProperty, value); } } class IdTextItem : BindableObject { public static BindableProperty IdProperty = CreateProperty>(nameof(Id)); public static BindableProperty TextProperty = CreateProperty>(nameof(Text)); public static BindableProperty DetailProperty = CreateProperty>(nameof(Detail)); public T Id { get => (T)GetValue(IdProperty); set => SetValue(IdProperty, value); } public string Text { get => (string)GetValue(TextProperty); set => SetValue(TextProperty, value); } public string? Detail { get => (string?)GetValue(DetailProperty); set => SetValue(DetailProperty, value); } }