Browse Source

fix: Issue #6263

pull/11418/head
Giuseppe Lippolis 3 years ago
parent
commit
74412481d8
  1. 43
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

43
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -5,9 +5,7 @@ using System.Collections.Specialized;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Xml.Linq;
using Avalonia.Controls.Selection; using Avalonia.Controls.Selection;
using Avalonia.Controls.Utils;
using Avalonia.Data; using Avalonia.Data;
using Avalonia.Input; using Avalonia.Input;
using Avalonia.Input.Platform; using Avalonia.Input.Platform;
@ -171,7 +169,7 @@ namespace Avalonia.Controls.Primitives
/// </summary> /// </summary>
public event EventHandler<SelectionChangedEventArgs>? SelectionChanged public event EventHandler<SelectionChangedEventArgs>? SelectionChanged
{ {
add => AddHandler(SelectionChangedEvent, value); add => AddHandler(SelectionChangedEvent, value);
remove => RemoveHandler(SelectionChangedEvent, value); remove => RemoveHandler(SelectionChangedEvent, value);
} }
@ -369,7 +367,7 @@ namespace Avalonia.Controls.Primitives
/// </summary> /// </summary>
public bool WrapSelection public bool WrapSelection
{ {
get => GetValue(WrapSelectionProperty); get => GetValue(WrapSelectionProperty);
set => SetValue(WrapSelectionProperty, value); set => SetValue(WrapSelectionProperty, value);
} }
@ -382,7 +380,7 @@ namespace Avalonia.Controls.Primitives
/// </remarks> /// </remarks>
protected SelectionMode SelectionMode protected SelectionMode SelectionMode
{ {
get => GetValue(SelectionModeProperty); get => GetValue(SelectionModeProperty);
set => SetValue(SelectionModeProperty, value); set => SetValue(SelectionModeProperty, value);
} }
@ -465,7 +463,10 @@ namespace Avalonia.Controls.Primitives
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{ {
base.OnAttachedToVisualTree(e); base.OnAttachedToVisualTree(e);
AutoScrollToSelectedItemIfNecessary(); if (Selection?.AnchorIndex is int index)
{
AutoScrollToSelectedItemIfNecessary(index);
}
} }
/// <inheritdoc /> /// <inheritdoc />
@ -476,7 +477,10 @@ namespace Avalonia.Controls.Primitives
void ExecuteScrollWhenLayoutUpdated(object? sender, EventArgs e) void ExecuteScrollWhenLayoutUpdated(object? sender, EventArgs e)
{ {
LayoutUpdated -= ExecuteScrollWhenLayoutUpdated; LayoutUpdated -= ExecuteScrollWhenLayoutUpdated;
AutoScrollToSelectedItemIfNecessary(); if (Selection?.AnchorIndex is int index)
{
AutoScrollToSelectedItemIfNecessary(index);
}
} }
if (AutoScrollToSelectedItem) if (AutoScrollToSelectedItem)
@ -657,7 +661,10 @@ namespace Avalonia.Controls.Primitives
if (change.Property == AutoScrollToSelectedItemProperty) if (change.Property == AutoScrollToSelectedItemProperty)
{ {
AutoScrollToSelectedItemIfNecessary(); if (Selection?.AnchorIndex is int index)
{
AutoScrollToSelectedItemIfNecessary(index);
}
} }
else if (change.Property == SelectionModeProperty && _selection is object) else if (change.Property == SelectionModeProperty && _selection is object)
{ {
@ -916,8 +923,11 @@ namespace Avalonia.Controls.Primitives
if (e.PropertyName == nameof(ISelectionModel.AnchorIndex)) if (e.PropertyName == nameof(ISelectionModel.AnchorIndex))
{ {
_hasScrolledToSelectedItem = false; _hasScrolledToSelectedItem = false;
KeyboardNavigation.SetTabOnceActiveElement(this, ContainerFromIndex(Selection.AnchorIndex)); if (Selection?.AnchorIndex is int index)
AutoScrollToSelectedItemIfNecessary(); {
KeyboardNavigation.SetTabOnceActiveElement(this, ContainerFromIndex(index));
AutoScrollToSelectedItemIfNecessary(index);
}
} }
else if (e.PropertyName == nameof(ISelectionModel.SelectedIndex) && _oldSelectedIndex != SelectedIndex) else if (e.PropertyName == nameof(ISelectionModel.SelectedIndex) && _oldSelectedIndex != SelectedIndex)
{ {
@ -1045,7 +1055,7 @@ namespace Avalonia.Controls.Primitives
return value; return value;
} }
else else
{ {
return AvaloniaProperty.UnsetValue; return AvaloniaProperty.UnsetValue;
} }
} }
@ -1103,16 +1113,19 @@ namespace Avalonia.Controls.Primitives
} }
} }
private void AutoScrollToSelectedItemIfNecessary() private void AutoScrollToSelectedItemIfNecessary(int anchorIndex)
{ {
if (AutoScrollToSelectedItem && if (AutoScrollToSelectedItem &&
!_hasScrolledToSelectedItem && !_hasScrolledToSelectedItem &&
Presenter is object && Presenter is object &&
Selection.AnchorIndex >= 0 && anchorIndex >= 0 &&
IsAttachedToVisualTree) IsAttachedToVisualTree)
{ {
ScrollIntoView(Selection.AnchorIndex); Dispatcher.UIThread.Post(state =>
_hasScrolledToSelectedItem = true; {
ScrollIntoView((int)state!);
_hasScrolledToSelectedItem = true;
}, anchorIndex);
} }
} }

Loading…
Cancel
Save