ListBox.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.Collections;
  4. using Avalonia.Controls.Generators;
  5. using Avalonia.Controls.Presenters;
  6. using Avalonia.Controls.Primitives;
  7. using Avalonia.Controls.Templates;
  8. using Avalonia.Input;
  9. namespace Avalonia.Controls
  10. {
  11. /// <summary>
  12. /// An <see cref="ItemsControl"/> in which individual items can be selected.
  13. /// </summary>
  14. public class ListBox : SelectingItemsControl
  15. {
  16. /// <summary>
  17. /// The default value for the <see cref="ItemsControl.ItemsPanel"/> property.
  18. /// </summary>
  19. private static readonly FuncTemplate<IPanel> DefaultPanel =
  20. new FuncTemplate<IPanel>(() => new VirtualizingStackPanel());
  21. /// <summary>
  22. /// Defines the <see cref="Scroll"/> property.
  23. /// </summary>
  24. public static readonly DirectProperty<ListBox, IScrollable> ScrollProperty =
  25. AvaloniaProperty.RegisterDirect<ListBox, IScrollable>(nameof(Scroll), o => o.Scroll);
  26. /// <summary>
  27. /// Defines the <see cref="SelectedItems"/> property.
  28. /// </summary>
  29. public static readonly new DirectProperty<SelectingItemsControl, IList> SelectedItemsProperty =
  30. SelectingItemsControl.SelectedItemsProperty;
  31. /// <summary>
  32. /// Defines the <see cref="SelectionMode"/> property.
  33. /// </summary>
  34. public static readonly new StyledProperty<SelectionMode> SelectionModeProperty =
  35. SelectingItemsControl.SelectionModeProperty;
  36. /// <summary>
  37. /// Defines the <see cref="VirtualizationMode"/> property.
  38. /// </summary>
  39. public static readonly StyledProperty<ItemVirtualizationMode> VirtualizationModeProperty =
  40. ItemsPresenter.VirtualizationModeProperty.AddOwner<ListBox>();
  41. private IScrollable _scroll;
  42. /// <summary>
  43. /// Initializes static members of the <see cref="ItemsControl"/> class.
  44. /// </summary>
  45. static ListBox()
  46. {
  47. ItemsPanelProperty.OverrideDefaultValue<ListBox>(DefaultPanel);
  48. VirtualizationModeProperty.OverrideDefaultValue<ListBox>(ItemVirtualizationMode.Simple);
  49. }
  50. /// <summary>
  51. /// Gets the scroll information for the <see cref="ListBox"/>.
  52. /// </summary>
  53. public IScrollable Scroll
  54. {
  55. get { return _scroll; }
  56. private set { SetAndRaise(ScrollProperty, ref _scroll, value); }
  57. }
  58. /// <inheritdoc/>
  59. public new IList SelectedItems
  60. {
  61. get => base.SelectedItems;
  62. set => base.SelectedItems = value;
  63. }
  64. /// <summary>
  65. /// Gets or sets the selection mode.
  66. /// </summary>
  67. /// <remarks>
  68. /// Note that the selection mode only applies to selections made via user interaction.
  69. /// Multiple selections can be made programatically regardless of the value of this property.
  70. /// </remarks>
  71. public new SelectionMode SelectionMode
  72. {
  73. get { return base.SelectionMode; }
  74. set { base.SelectionMode = value; }
  75. }
  76. /// <summary>
  77. /// Gets or sets the virtualization mode for the items.
  78. /// </summary>
  79. public ItemVirtualizationMode VirtualizationMode
  80. {
  81. get { return GetValue(VirtualizationModeProperty); }
  82. set { SetValue(VirtualizationModeProperty, value); }
  83. }
  84. /// <summary>
  85. /// Selects all items in the <see cref="ListBox"/>.
  86. /// </summary>
  87. public new void SelectAll() => base.SelectAll();
  88. /// <summary>
  89. /// Deselects all items in the <see cref="ListBox"/>.
  90. /// </summary>
  91. public new void UnselectAll() => base.UnselectAll();
  92. /// <inheritdoc/>
  93. protected override IItemContainerGenerator CreateItemContainerGenerator()
  94. {
  95. return new ItemContainerGenerator<ListBoxItem>(
  96. this,
  97. ListBoxItem.ContentProperty,
  98. ListBoxItem.ContentTemplateProperty);
  99. }
  100. /// <inheritdoc/>
  101. protected override void OnGotFocus(GotFocusEventArgs e)
  102. {
  103. base.OnGotFocus(e);
  104. if (e.NavigationMethod == NavigationMethod.Directional)
  105. {
  106. e.Handled = UpdateSelectionFromEventSource(
  107. e.Source,
  108. true,
  109. (e.InputModifiers & InputModifiers.Shift) != 0);
  110. }
  111. }
  112. /// <inheritdoc/>
  113. protected override void OnPointerPressed(PointerPressedEventArgs e)
  114. {
  115. base.OnPointerPressed(e);
  116. if (e.MouseButton == MouseButton.Left || e.MouseButton == MouseButton.Right)
  117. {
  118. e.Handled = UpdateSelectionFromEventSource(
  119. e.Source,
  120. true,
  121. (e.InputModifiers & InputModifiers.Shift) != 0,
  122. (e.InputModifiers & InputModifiers.Control) != 0,
  123. e.MouseButton == MouseButton.Right);
  124. }
  125. }
  126. protected override void OnTemplateApplied(TemplateAppliedEventArgs e)
  127. {
  128. base.OnTemplateApplied(e);
  129. Scroll = e.NameScope.Find<IScrollable>("PART_ScrollViewer");
  130. }
  131. }
  132. }