using System.Collections;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Selection;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Input.Platform;
using Avalonia.VisualTree;
namespace Avalonia.Controls
{
///
/// An in which individual items can be selected.
///
[TemplatePart("PART_ScrollViewer", typeof(IScrollable))]
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;
}
///
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 programmatically 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.Clear();
///
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.HasAllFlags(KeyModifiers.Shift),
e.KeyModifiers.HasAllFlags(KeyModifiers.Control));
}
}
///
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.HasAllFlags(KeyModifiers.Shift),
e.KeyModifiers.HasAllFlags(AvaloniaLocator.Current.GetRequiredService().CommandModifiers),
point.Properties.IsRightButtonPressed);
}
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
Scroll = e.NameScope.Find("PART_ScrollViewer");
}
}
}