| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- // Copyright (c) The Avalonia Project. All rights reserved.
- // Licensed under the MIT license. See licence.md file in the project root for full license information.
- using System.Collections;
- using Avalonia.Controls.Generators;
- using Avalonia.Controls.Presenters;
- using Avalonia.Controls.Primitives;
- using Avalonia.Controls.Templates;
- using Avalonia.Input;
- namespace Avalonia.Controls
- {
- /// <summary>
- /// An <see cref="ItemsControl"/> in which individual items can be selected.
- /// </summary>
- public class ListBox : SelectingItemsControl
- {
- /// <summary>
- /// The default value for the <see cref="ItemsControl.ItemsPanel"/> property.
- /// </summary>
- private static readonly FuncTemplate<IPanel> DefaultPanel =
- new FuncTemplate<IPanel>(() => new VirtualizingStackPanel());
- /// <summary>
- /// Defines the <see cref="Scroll"/> property.
- /// </summary>
- public static readonly DirectProperty<ListBox, IScrollable> ScrollProperty =
- AvaloniaProperty.RegisterDirect<ListBox, IScrollable>(nameof(Scroll), o => o.Scroll);
- /// <summary>
- /// Defines the <see cref="SelectedItems"/> property.
- /// </summary>
- public static readonly new DirectProperty<SelectingItemsControl, IList> SelectedItemsProperty =
- SelectingItemsControl.SelectedItemsProperty;
- /// <summary>
- /// Defines the <see cref="SelectionMode"/> property.
- /// </summary>
- public static readonly new StyledProperty<SelectionMode> SelectionModeProperty =
- SelectingItemsControl.SelectionModeProperty;
- /// <summary>
- /// Defines the <see cref="VirtualizationMode"/> property.
- /// </summary>
- public static readonly StyledProperty<ItemVirtualizationMode> VirtualizationModeProperty =
- ItemsPresenter.VirtualizationModeProperty.AddOwner<ListBox>();
- private IScrollable _scroll;
- /// <summary>
- /// Initializes static members of the <see cref="ItemsControl"/> class.
- /// </summary>
- static ListBox()
- {
- ItemsPanelProperty.OverrideDefaultValue<ListBox>(DefaultPanel);
- VirtualizationModeProperty.OverrideDefaultValue<ListBox>(ItemVirtualizationMode.Simple);
- }
- /// <summary>
- /// Gets the scroll information for the <see cref="ListBox"/>.
- /// </summary>
- public IScrollable Scroll
- {
- get { return _scroll; }
- private set { SetAndRaise(ScrollProperty, ref _scroll, value); }
- }
- /// <inheritdoc/>
- public new IList SelectedItems
- {
- get => base.SelectedItems;
- set => base.SelectedItems = value;
- }
- /// <summary>
- /// Gets or sets the selection mode.
- /// </summary>
- /// <remarks>
- /// 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.
- /// </remarks>
- public new SelectionMode SelectionMode
- {
- get { return base.SelectionMode; }
- set { base.SelectionMode = value; }
- }
- /// <summary>
- /// Gets or sets the virtualization mode for the items.
- /// </summary>
- public ItemVirtualizationMode VirtualizationMode
- {
- get { return GetValue(VirtualizationModeProperty); }
- set { SetValue(VirtualizationModeProperty, value); }
- }
- /// <summary>
- /// Selects all items in the <see cref="ListBox"/>.
- /// </summary>
- public new void SelectAll() => base.SelectAll();
- /// <summary>
- /// Deselects all items in the <see cref="ListBox"/>.
- /// </summary>
- public new void UnselectAll() => base.UnselectAll();
- /// <inheritdoc/>
- protected override IItemContainerGenerator CreateItemContainerGenerator()
- {
- return new ItemContainerGenerator<ListBoxItem>(
- this,
- ListBoxItem.ContentProperty,
- ListBoxItem.ContentTemplateProperty);
- }
- /// <inheritdoc/>
- protected override void OnGotFocus(GotFocusEventArgs e)
- {
- base.OnGotFocus(e);
- if (e.NavigationMethod == NavigationMethod.Directional)
- {
- e.Handled = UpdateSelectionFromEventSource(
- e.Source,
- true,
- (e.InputModifiers & InputModifiers.Shift) != 0);
- }
- }
- /// <inheritdoc/>
- protected override void OnPointerPressed(PointerPressedEventArgs e)
- {
- base.OnPointerPressed(e);
- if (e.MouseButton == MouseButton.Left || e.MouseButton == MouseButton.Right)
- {
- e.Handled = UpdateSelectionFromEventSource(
- e.Source,
- true,
- (e.InputModifiers & InputModifiers.Shift) != 0,
- (e.InputModifiers & InputModifiers.Control) != 0,
- e.MouseButton == MouseButton.Right);
- }
- }
- protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
- {
- base.OnTemplateApplied(e);
- Scroll = e.NameScope.Find<IScrollable>("PART_ScrollViewer");
- }
- }
- }
|