-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix some issues on SelectingItemsControls when items or control changes visibility #20798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
2cc2a91
460e953
2b7368c
ea3caf7
84c73d1
f80df33
5e61036
b28138e
608bc93
232902c
d214ee4
7357120
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -461,7 +461,7 @@ private protected override void OnItemsViewCollectionChanged(object? sender, Not | |
|
|
||
| if (AlwaysSelected && SelectedIndex == -1 && ItemCount > 0) | ||
| { | ||
| SelectedIndex = 0; | ||
| SelectedIndex = GetFirstVisibleAndEnabledIndex(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -534,6 +534,11 @@ protected internal override void ContainerForItemPreparedOverride(Control contai | |
|
|
||
| if (Selection.AnchorIndex == index) | ||
| KeyboardNavigation.SetTabOnceActiveElement(this, container); | ||
|
|
||
| if (AlwaysSelected && index == SelectedIndex && (!container.IsVisible || !container.IsEnabled)) | ||
| { | ||
| MoveSelectionToFirstVisibleAndEnabledItem(); | ||
| } | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
|
|
@@ -616,6 +621,13 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang | |
| { | ||
| AutoScrollToSelectedItemIfNecessary(GetAnchorIndex()); | ||
| } | ||
| else if (change.Property == IsVisibleProperty) | ||
| { | ||
| if (change.GetNewValue<bool>()) | ||
| { | ||
| AutoScrollToSelectedItemIfNecessary(GetAnchorIndex()); | ||
| } | ||
| } | ||
| else if (change.Property == SelectionModeProperty && _selection is object) | ||
| { | ||
| var newValue = change.GetNewValue<SelectionMode>(); | ||
|
|
@@ -1035,7 +1047,7 @@ private void OnSelectionModelLostSelection(object? sender, EventArgs e) | |
| { | ||
| if (AlwaysSelected && ItemsView.Count > 0) | ||
| { | ||
| SelectedIndex = 0; | ||
| SelectedIndex = GetFirstVisibleAndEnabledIndex(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -1153,6 +1165,11 @@ Presenter is object && | |
| anchorIndex >= 0 && | ||
| IsAttachedToVisualTree) | ||
| { | ||
| if (!IsEffectivelyVisible) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Dispatcher.UIThread.Post(state => | ||
| { | ||
| ScrollIntoView((int)state!); | ||
|
|
@@ -1205,6 +1222,54 @@ private void MarkContainerSelected(Control container, bool selected) | |
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Finds the first visible and enabled index in the ItemsSource. | ||
| /// </summary> | ||
| /// <returns>the index of the first visible and enabled item, or -1 if none found</returns> | ||
| private int GetFirstVisibleAndEnabledIndex() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll place general comments here. Without testing, my understanding of WPF is:
Part 1 likely doesn't function the same since you are treating Enabled/Visible as equivalent -- they have slight differences. Visible=false
Enabled=false
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These tabs can be selected programatically. I am not sure how to deal with that if really all tabs are either disabled or hidden. We should review this in the team before making an decision. |
||
| { | ||
| var count = ItemCount; | ||
| if (count == 0) | ||
| return -1; | ||
|
|
||
| for (var i = 0; i < count; i++) | ||
| { | ||
| var container = ContainerFromIndex(i); | ||
| if (container is not null) | ||
| { | ||
| if (container is { IsVisible: true, IsEnabled: true }) | ||
| return i; | ||
| } | ||
| else | ||
| { | ||
| var item = ItemsView[i]; | ||
| if (item is Visual v) | ||
| { | ||
| if (v.IsVisible && (v is not Control c || c.IsEnabled)) | ||
| return i; | ||
| } | ||
| else if (item is not null) | ||
| { | ||
| return i; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return -1; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// this method moves selection to first visible and enabled item. | ||
| /// </summary> | ||
| private void MoveSelectionToFirstVisibleAndEnabledItem() | ||
| { | ||
| var index = GetFirstVisibleAndEnabledIndex(); | ||
| if (index != -1 && index != SelectedIndex) | ||
| { | ||
| SelectedIndex = index; | ||
| } | ||
| } | ||
|
|
||
| private void UpdateContainerSelection() | ||
| { | ||
| if (Presenter?.Panel is { } panel) | ||
|
|
@@ -1251,7 +1316,7 @@ private void InitializeSelectionModel(ISelectionModel model) | |
|
|
||
| if (_updateState is null && AlwaysSelected && model.Count == 0) | ||
| { | ||
| model.SelectedIndex = 0; | ||
| model.SelectedIndex = GetFirstVisibleAndEnabledIndex(); | ||
| } | ||
|
|
||
| UpdateContainerSelection(); | ||
|
|
@@ -1358,7 +1423,7 @@ private void EndUpdating() | |
|
|
||
| if (AlwaysSelected && SelectedIndex == -1 && ItemCount > 0) | ||
| { | ||
| SelectedIndex = 0; | ||
| SelectedIndex = GetFirstVisibleAndEnabledIndex(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.