- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.5k
 
Keep SelectingItemsControl selection values until ItemsSource is set #18634
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
Keep SelectingItemsControl selection values until ItemsSource is set #18634
Conversation
| 
           You can test this PR using the following package version.   | 
    
| 
           Hey @MrJul , 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   | 
    
…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
…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
| 
           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?  | 
    
| 
           I believe this is by desgin  | 
    
What does the pull request do?
This PR keeps the values for
SelectedItem,SelectedIndexandSelectedValuefromSelectingItemsControlunchanged until anItemsSourceis set.Note that this PR does not change the behavior if the
ItemsSourceproperty is set to a valid collection which is only populated later.What is the current behavior?
Setting a selection property such as
SelectedItembefore theItemsSourceis 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
ItemsSourceavailable 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
ItemsSourceis set.How was the solution implemented (if it's not obvious)?
For
SelectedItemandSelectedIndex, which are part of theISelectionModel, Avalonia simply waits to setSelectionModel.ItemsSourceuntil we have a properItemsControl.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 matchingSelectedItemright before setting the source (commits 3 and 4).Notes
Copying the comments I left in
SelectingItemsControl:InternalSelectionModelkeeps theSelectedIndexandSelectedItemvalues before theItemsSourceis set. However,SelectedValueisn't part of that model, so we have to set theSelectedItemfromSelectedValuemanually 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, forBeginInit/EndInit)InternalSelectionModel(SelectedIndex/SelectedItem)SelectedItemsControl(SelectedValue)There's the opportunity to have a single place responsible for this logic.
Fixed issues
ComboBox.SelectedItemlost on control generated in a template #14654