Skip to content

Conversation

@MrJul
Copy link
Member

@MrJul MrJul commented Apr 12, 2025

What does the pull request do?

This PR keeps the values for SelectedItem, SelectedIndex and SelectedValue from SelectingItemsControl unchanged until an ItemsSource is set.

Note that this PR does not change the behavior if the ItemsSource property is set to a valid collection which is only populated later.

What is the current behavior?

Setting a selection property such as SelectedItem before the ItemsSource is set immediately resets it to its default value, because the item/index/value doesn't exist inside the list.

There's already code in place to avoid this issue when XAML initialization takes place. However, it's frequent to have the ItemsSource available only after initialization has completed (for example through a relative binding).

What is the updated/expected behavior with this PR?

The selection values are kept until ItemsSource is set.

How was the solution implemented (if it's not obvious)?

For SelectedItem and SelectedIndex, which are part of the ISelectionModel, Avalonia simply waits to set SelectionModel.ItemsSource until we have a proper ItemsControl.ItemsSource (commits 1 and 2). There's already code in place to automatically set the correct selection properties as soon as the items source is set.

For SelectedValue, this isn't the case. We need to retrieve the matching SelectedItem right before setting the source (commits 3 and 4).

Notes

Copying the comments I left in SelectingItemsControl:

InternalSelectionModel keeps the SelectedIndex and SelectedItem values before the ItemsSource is set. However, SelectedValue isn't part of that model, so we have to set the SelectedItem from SelectedValue manually now that we have a source.

While this works, this is messy: we effectively have "lazy selection initialization" in 3 places:

  • UpdateState (all selection properties, for BeginInit/EndInit)
  • InternalSelectionModel (SelectedIndex/SelectedItem)
  • SelectedItemsControl (SelectedValue)

There's the opportunity to have a single place responsible for this logic.

Fixed issues

@MrJul MrJul added bug backport-candidate-11.2.x Consider this PR for backporting to 11.2 branch backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch customer-priority Issue reported by a customer with a support agreement. labels Apr 12, 2025
@avaloniaui-bot
Copy link

You can test this PR using the following package version. 12.0.999-cibuild0056011-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@timunie
Copy link
Contributor

timunie commented Apr 12, 2025

Hey @MrJul ,

If a control is unloaded or DataContext changes, does this also prevent clearing selection in case ItemsSource is changed before SelectedItem?

@MrJul
Copy link
Member Author

MrJul commented Apr 12, 2025

If a control is unloaded or DataContext changes, does this also prevent clearing selection in case ItemsSource is changed before SelectedItem?

No, unfortunately this PR only prevents clearing the selection before ItemsSource is set the very first time, nothing more.

@MrJul MrJul added this pull request to the merge queue Apr 18, 2025
Merged via the queue into AvaloniaUI:master with commit 2d0b49a Apr 18, 2025
11 checks passed
@MrJul MrJul deleted the fix/itemscontrol-uninitialized-items-source branch April 18, 2025 09:59
MrJul added a commit that referenced this pull request Apr 20, 2025
…18634)

* Add failing tests for SelectedItem/SelectedIndex without an ItemsSource

* Keep SelectedItem/SelectedIndex until ItemsSource is set

* Add failing tests for setting SelectedValue without an ItemsSource

* Keep SelectedValue until ItemsSource is set
@MrJul MrJul added backported-11.2.x and removed backport-candidate-11.2.x Consider this PR for backporting to 11.2 branch labels Apr 20, 2025
MrJul added a commit that referenced this pull request Apr 25, 2025
…18634)

* Add failing tests for SelectedItem/SelectedIndex without an ItemsSource

* Keep SelectedItem/SelectedIndex until ItemsSource is set

* Add failing tests for setting SelectedValue without an ItemsSource

* Keep SelectedValue until ItemsSource is set
@MrJul MrJul added backported-11.3.x and removed backport-candidate-11.3.x Consider this PR for backporting to 11.3 branch labels Apr 25, 2025
@mjo151
Copy link

mjo151 commented Apr 25, 2025

After updating from 11.2.7 to 11.2.8, I noticed a change in behavior and I believe it's caused by the changes in this issue. The TabControl.SelectionChanged event is no longer raised when the tab control first loads. Is that an intended change in behavior or a bug introduced in 11.2.8?

@timunie
Copy link
Contributor

timunie commented May 11, 2025

I believe this is by desgin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backported-11.2.x backported-11.3.x bug customer-priority Issue reported by a customer with a support agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Datagrid ComboBox SelectedItem Show Error ComboBox.SelectedItem lost on control generated in a template

5 participants