|
|
@@ -2,9 +2,12 @@
|
|
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|
|
|
|
|
using System;
|
|
|
+using System.Linq;
|
|
|
using Avalonia.Controls.Generators;
|
|
|
+using Avalonia.Controls.Presenters;
|
|
|
using Avalonia.Controls.Primitives;
|
|
|
using Avalonia.Controls.Shapes;
|
|
|
+using Avalonia.Controls.Templates;
|
|
|
using Avalonia.Input;
|
|
|
using Avalonia.LogicalTree;
|
|
|
using Avalonia.Media;
|
|
|
@@ -12,12 +15,17 @@ using Avalonia.VisualTree;
|
|
|
|
|
|
namespace Avalonia.Controls
|
|
|
{
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// A drop-down list control.
|
|
|
/// </summary>
|
|
|
public class DropDown : 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="IsDropDownOpen"/> property.
|
|
|
/// </summary>
|
|
|
@@ -39,6 +47,12 @@ namespace Avalonia.Controls
|
|
|
public static readonly DirectProperty<DropDown, object> SelectionBoxItemProperty =
|
|
|
AvaloniaProperty.RegisterDirect<DropDown, object>(nameof(SelectionBoxItem), o => o.SelectionBoxItem);
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Defines the <see cref="VirtualizationMode"/> property.
|
|
|
+ /// </summary>
|
|
|
+ public static readonly StyledProperty<ItemVirtualizationMode> VirtualizationModeProperty =
|
|
|
+ ItemsPresenter.VirtualizationModeProperty.AddOwner<DropDown>();
|
|
|
+
|
|
|
private bool _isDropDownOpen;
|
|
|
private Popup _popup;
|
|
|
private object _selectionBoxItem;
|
|
|
@@ -48,6 +62,7 @@ namespace Avalonia.Controls
|
|
|
/// </summary>
|
|
|
static DropDown()
|
|
|
{
|
|
|
+ ItemsPanelProperty.OverrideDefaultValue<DropDown>(DefaultPanel);
|
|
|
FocusableProperty.OverrideDefaultValue<DropDown>(true);
|
|
|
SelectedItemProperty.Changed.AddClassHandler<DropDown>(x => x.SelectedItemChanged);
|
|
|
KeyDownEvent.AddClassHandler<DropDown>(x => x.OnKeyDown, Interactivity.RoutingStrategies.Tunnel);
|
|
|
@@ -80,6 +95,15 @@ namespace Avalonia.Controls
|
|
|
set { SetAndRaise(SelectionBoxItemProperty, ref _selectionBoxItem, value); }
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// Gets or sets the virtualization mode for the items.
|
|
|
+ /// </summary>
|
|
|
+ public ItemVirtualizationMode VirtualizationMode
|
|
|
+ {
|
|
|
+ get { return GetValue(VirtualizationModeProperty); }
|
|
|
+ set { SetValue(VirtualizationModeProperty, value); }
|
|
|
+ }
|
|
|
+
|
|
|
/// <inheritdoc/>
|
|
|
protected override IItemContainerGenerator CreateItemContainerGenerator()
|
|
|
{
|
|
|
@@ -138,6 +162,16 @@ namespace Avalonia.Controls
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
}
|
|
|
+ else if (IsDropDownOpen && SelectedIndex < 0 && ItemCount > 0 &&
|
|
|
+ (e.Key == Key.Up || e.Key == Key.Down))
|
|
|
+ {
|
|
|
+ var firstChild = Presenter?.Panel?.Children.FirstOrDefault(c => CanFocus(c));
|
|
|
+ if (firstChild != null)
|
|
|
+ {
|
|
|
+ FocusManager.Instance?.Focus(firstChild, NavigationMethod.Directional);
|
|
|
+ e.Handled = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
@@ -159,6 +193,7 @@ namespace Avalonia.Controls
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
base.OnPointerPressed(e);
|
|
|
}
|
|
|
|
|
|
@@ -168,28 +203,65 @@ namespace Avalonia.Controls
|
|
|
if (_popup != null)
|
|
|
{
|
|
|
_popup.Opened -= PopupOpened;
|
|
|
+ _popup.Closed -= PopupClosed;
|
|
|
}
|
|
|
|
|
|
_popup = e.NameScope.Get<Popup>("PART_Popup");
|
|
|
_popup.Opened += PopupOpened;
|
|
|
+ _popup.Closed += PopupClosed;
|
|
|
+
|
|
|
+ base.OnTemplateApplied(e);
|
|
|
}
|
|
|
|
|
|
- private void PopupOpened(object sender, EventArgs e)
|
|
|
+ internal void ItemFocused(DropDownItem dropDownItem)
|
|
|
{
|
|
|
- var selectedIndex = SelectedIndex;
|
|
|
+ if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
|
|
|
+ {
|
|
|
+ dropDownItem.BringIntoView();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- if (selectedIndex != -1)
|
|
|
+ private void PopupClosed(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ if (CanFocus(this))
|
|
|
{
|
|
|
- var container = ItemContainerGenerator.ContainerFromIndex(selectedIndex);
|
|
|
- container?.Focus();
|
|
|
+ Focus();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void PopupOpened(object sender, EventArgs e)
|
|
|
+ {
|
|
|
+ TryFocusSelectedItem();
|
|
|
+ }
|
|
|
+
|
|
|
private void SelectedItemChanged(AvaloniaPropertyChangedEventArgs e)
|
|
|
{
|
|
|
UpdateSelectionBoxItem(e.NewValue);
|
|
|
+ TryFocusSelectedItem();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void TryFocusSelectedItem()
|
|
|
+ {
|
|
|
+ var selectedIndex = SelectedIndex;
|
|
|
+ if (IsDropDownOpen && selectedIndex != -1)
|
|
|
+ {
|
|
|
+ var container = ItemContainerGenerator.ContainerFromIndex(selectedIndex);
|
|
|
+
|
|
|
+ if(container == null && SelectedItems.Count > 0)
|
|
|
+ {
|
|
|
+ ScrollIntoView(SelectedItems[0]);
|
|
|
+ container = ItemContainerGenerator.ContainerFromIndex(selectedIndex);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (container != null && CanFocus(container))
|
|
|
+ {
|
|
|
+ container.Focus();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ private bool CanFocus(IControl control) => control.Focusable && control.IsEnabledCore && control.IsVisible;
|
|
|
+
|
|
|
private void UpdateSelectionBoxItem(object item)
|
|
|
{
|
|
|
var contentControl = item as IContentControl;
|
|
|
@@ -219,7 +291,8 @@ namespace Avalonia.Controls
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- SelectionBoxItem = item;
|
|
|
+ var selector = MemberSelector;
|
|
|
+ SelectionBoxItem = selector != null ? selector.Select(item) : item;
|
|
|
}
|
|
|
}
|
|
|
|