Browse Source

Improve TreeView.AutoScrollToSelectedItem.

It's still of very limited use as the selected item's ancestors need to be manually expanded, but at least when the selected item is realized it should now be scrolled into view.

Fixes #10449
pull/11629/head
Steven Kirk 3 years ago
parent
commit
f912d4ca28
  1. 11
      src/Avalonia.Controls/TreeView.cs

11
src/Avalonia.Controls/TreeView.cs

@ -84,6 +84,11 @@ namespace Avalonia.Controls
/// <summary>
/// Gets or sets a value indicating whether to automatically scroll to newly selected items.
/// </summary>
/// <remarks>
/// This property is of limited use with <see cref="TreeView"/> as it will only scroll
/// to realized items. To scroll to a non-expanded item, you need to ensure that its
/// ancestors are expanded.
/// </remarks>
public bool AutoScrollToSelectedItem
{
get => GetValue(AutoScrollToSelectedItemProperty);
@ -531,6 +536,12 @@ namespace Avalonia.Controls
// The IsSelected property is not set on the container: update the container
// selection based on the current selection as understood by this control.
MarkContainerSelected(container, SelectedItems.Contains(item));
// If the newly realized container is the selected container, scroll to it after layout.
if (AutoScrollToSelectedItem && SelectedItem == item)
{
Dispatcher.UIThread.Post(container.BringIntoView, DispatcherPriority.Loaded);
}
}
/// <inheritdoc/>

Loading…
Cancel
Save