-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix TabItem's Content inheriting TabControl's DataContext instead of TabItem's #20541
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
Conversation
|
You can test this PR using the following package version. |
src/Avalonia.Controls/TabControl.cs
Outdated
| } | ||
| } | ||
|
|
||
| protected internal override void ContainerForItemPreparedOverride(Control container, object? item, int index) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please explain why this was moved to ContainerForItemPreparedOverride? The DataContext should probably be set asap and PrepareContainerForItemOverride seems like the right place for that.
Reverting this still makes the new test passes so I'm a bit confused.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I must have gotten confused in my attempts to understand what's happening. Good catch!
|
You can test this PR using the following package version. |
MrJul
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thank you!
What does the pull request do?
Fixes the TabItem's Content inheriting TabControl's DataContext instead of TabItem's.
What is the current behavior?
When the
TabItemcontent'sDataContextis not explicitly set, it inherits theDataContextfromTabControlafterUpdateSelectedContent.TabItem's Content inheritsDataContextfromTabItemafter getting added to logical tree (TabControl's Items)TabItem's Content has itsInheritanceParentset toTabControl.ContentPart(ContentPresenter) whenTabItemis selected.TabItem's ContentDataContexteffectively inheritTabControl'sDataContextWhat is the updated/expected behavior with this PR?
The
TabItem's Content keeps itsDataContextinherited fromTabItem.How was the solution implemented (if it's not obvious)?
First, the initialUpdateSelectedContentcall is changed to happen inContainerForItemPreparedOverriderather thanPrepareContainerForItemOverrideso that theTabItemContent'sDataContextcan be inherited fromTabItemafter getting added to the logical tree.Then, when
UpdateSelectedContentis called the original Content'sDataContextis saved to a variable before changing itsInheritanceParenttoContentPart. The savedDataContextis reapplied after. See code comments for further details.Checklist
Breaking changes
Perhaps changing the expectation ofUpdateSelectedContentbeing called fromContainerForItemPreparedOverrideinstead?Obsoletions / Deprecations
Fixed issues
Fixes #10958