Browse Source

Scroll and focus to selected item when dropdown is shown

pull/2122/head
Andrey Kunchev 8 years ago
parent
commit
a297f00863
  1. 8
      src/Avalonia.Controls/DropDown.cs
  2. 32
      src/Avalonia.Controls/DropDownItem.cs

8
src/Avalonia.Controls/DropDown.cs

@ -190,6 +190,14 @@ namespace Avalonia.Controls
base.OnTemplateApplied(e);
}
internal void ItemFocused(DropDownItem dropDownItem)
{
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
{
dropDownItem.BringIntoView();
}
}
private void PopupClosed(object sender, EventArgs e)
{
if (Focusable)

32
src/Avalonia.Controls/DropDownItem.cs

@ -2,43 +2,19 @@
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Reactive.Linq;
namespace Avalonia.Controls
{
/// <summary>
/// A selectable item in a <see cref="DropDown"/>.
/// </summary>
public class DropDownItem : ContentControl, ISelectable
public class DropDownItem : ListBoxItem
{
/// <summary>
/// Defines the <see cref="IsSelected"/> property.
/// </summary>
public static readonly StyledProperty<bool> IsSelectedProperty =
AvaloniaProperty.Register<DropDownItem, bool>(nameof(IsSelected));
/// <summary>
/// Initializes static members of the <see cref="DropDownItem"/> class.
/// </summary>
static DropDownItem()
{
FocusableProperty.OverrideDefaultValue<DropDownItem>(true);
}
public DropDownItem()
{
this.GetObservable(DropDownItem.IsFocusedProperty).Subscribe(focused =>
{
PseudoClasses.Set(":selected", focused);
});
}
/// <summary>
/// Gets or sets the selection state of the item.
/// </summary>
public bool IsSelected
{
get { return GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
this.GetObservable(DropDownItem.IsFocusedProperty).Where(focused => focused)
.Subscribe(_ => (Parent as DropDown)?.ItemFocused(this));
}
}
}

Loading…
Cancel
Save