// 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
{
///
/// 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 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 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 new void SelectAll() => base.SelectAll();
///
/// Deselects all items in the .
///
public new void UnselectAll() => base.UnselectAll();
///
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.InputModifiers & InputModifiers.Shift) != 0);
}
}
///
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("PART_ScrollViewer");
}
}
}