Skip to content

Conversation

@ptasev
Copy link
Contributor

@ptasev ptasev commented Jan 26, 2026

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 TabItem content's DataContext is not explicitly set, it inherits the DataContext from TabControl after UpdateSelectedContent.

  1. TabItem's Content inherits DataContext from TabItem after getting added to logical tree (TabControl's Items)
  2. TabItem's Content has its InheritanceParent set to TabControl.ContentPart (ContentPresenter) when TabItem is selected.
  3. This makes the TabItem's Content DataContext effectively inherit TabControl's DataContext

What is the updated/expected behavior with this PR?

The TabItem's Content keeps its DataContext inherited from TabItem.

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

First, the initial UpdateSelectedContent call is changed to happen in ContainerForItemPreparedOverride rather than PrepareContainerForItemOverride so that the TabItem Content's DataContext can be inherited from TabItem after getting added to the logical tree.

Then, when UpdateSelectedContent is called the original Content's DataContext is saved to a variable before changing its InheritanceParent to ContentPart. The saved DataContext is reapplied after. See code comments for further details.

Checklist

Breaking changes

Perhaps changing the expectation of UpdateSelectedContent being called from ContainerForItemPreparedOverride instead?

Obsoletions / Deprecations

Fixed issues

Fixes #10958

@avaloniaui-bot
Copy link

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

@MrJul MrJul added the bug label Jan 26, 2026
}
}

protected internal override void ContainerForItemPreparedOverride(Control container, object? item, int index)
Copy link
Member

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.

Copy link
Contributor Author

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!

@ptasev ptasev requested a review from MrJul February 10, 2026 20:03
@avaloniaui-bot
Copy link

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

Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

@MrJul MrJul added this pull request to the merge queue Feb 11, 2026
Merged via the queue into AvaloniaUI:master with commit b57223e Feb 11, 2026
11 checks passed
@ptasev ptasev deleted the pt/tabitem-child-datacontext branch February 11, 2026 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TabItem not passing its DataContext to its child view

3 participants