using System.Collections; using Avalonia.Controls.Generators; using Avalonia.Controls.Presenters; using Avalonia.Controls.Primitives; using Avalonia.Controls.Templates; using Avalonia.Input; using Avalonia.VisualTree; namespace Avalonia.Controls { /// /// An in which individual items can be selected. /// public class ListBox : SelectingItemsControl { /// /// The default value for the property. /// private static readonly FuncTemplate DefaultPanel = new FuncTemplate(() => new VirtualizingStackPanel()); /// /// Defines the property. /// public static readonly DirectProperty ScrollProperty = AvaloniaProperty.RegisterDirect(nameof(Scroll), o => o.Scroll); /// /// Defines the property. /// public static readonly new DirectProperty SelectedItemsProperty = SelectingItemsControl.SelectedItemsProperty; /// /// Defines the property. /// public static readonly new DirectProperty SelectionProperty = SelectingItemsControl.SelectionProperty; /// /// Defines the property. /// public static readonly new StyledProperty SelectionModeProperty = SelectingItemsControl.SelectionModeProperty; /// /// Defines the property. /// public static readonly StyledProperty VirtualizationModeProperty = ItemsPresenter.VirtualizationModeProperty.AddOwner(); private IScrollable _scroll; /// /// Initializes static members of the class. /// static ListBox() { ItemsPanelProperty.OverrideDefaultValue(DefaultPanel); VirtualizationModeProperty.OverrideDefaultValue(ItemVirtualizationMode.Simple); } /// /// Gets the scroll information for the . /// public IScrollable Scroll { get { return _scroll; } private set { SetAndRaise(ScrollProperty, ref _scroll, value); } } /// public new IList SelectedItems { get => base.SelectedItems; set => base.SelectedItems = value; } /// /// Gets or sets a model holding the current selection. /// public new ISelectionModel Selection { get => base.Selection; set => base.Selection = value; } /// /// Gets or sets the selection mode. /// /// /// Note that the selection mode only applies to selections made via user interaction. /// Multiple selections can be made programatically regardless of the value of this property. /// public new SelectionMode SelectionMode { get { return base.SelectionMode; } set { base.SelectionMode = value; } } /// /// Gets or sets the virtualization mode for the items. /// public ItemVirtualizationMode VirtualizationMode { get { return GetValue(VirtualizationModeProperty); } set { SetValue(VirtualizationModeProperty, value); } } /// /// Selects all items in the . /// public void SelectAll() => Selection.SelectAll(); /// /// Deselects all items in the . /// public void UnselectAll() => Selection.ClearSelection(); /// protected override IItemContainerGenerator CreateItemContainerGenerator() { return new ItemContainerGenerator( this, ListBoxItem.ContentProperty, ListBoxItem.ContentTemplateProperty); } /// protected override void OnGotFocus(GotFocusEventArgs e) { base.OnGotFocus(e); if (e.NavigationMethod == NavigationMethod.Directional) { e.Handled = UpdateSelectionFromEventSource( e.Source, true, (e.KeyModifiers & KeyModifiers.Shift) != 0); } } /// protected override void OnPointerPressed(PointerPressedEventArgs e) { base.OnPointerPressed(e); if (e.Source is IVisual source) { var point = e.GetCurrentPoint(source); if (point.Properties.IsLeftButtonPressed || point.Properties.IsRightButtonPressed) { e.Handled = UpdateSelectionFromEventSource( e.Source, true, (e.KeyModifiers & KeyModifiers.Shift) != 0, (e.KeyModifiers & KeyModifiers.Control) != 0, point.Properties.IsRightButtonPressed); } } } protected override void OnTemplateApplied(TemplateAppliedEventArgs e) { Scroll = e.NameScope.Find("PART_ScrollViewer"); base.OnTemplateApplied(e); } } }