ContentPresenter
should not become a Parent
#4894
Labels
difficulty/challenging 🤯
Categorizes an issue for which the difficulty level is reachable with internals understanding
kind/bug
Something isn't working
project/layout 🧱
Categorizes an issue or PR as relevant to layouting and containers (Measure/Arrange, Collections,..)
Current behavior
When
Content
ofContentPresenter
is set, theContentPresenter
becomes its parent, hence the content then inheritsContentPresenter
'sDataContext
.Expected behavior
ContentPresenter
should not become anybody's parentContentPresenter.Content
will throw an exception (as the control is already displayed)ContentPresenter.Content
should not inheritDataContext
after being set (and should retain its pre-existingDataContext
if such exists), but should inherit it if theDataContext
changes later while it is set 🤪ContentPresenter
's content should still be part of its visual tree (discoverable viaVisualTreeHelper
How to reproduce it (as minimally and precisely as possible)
TestControl.cs:
MainPage.xaml:
MainPage.xaml.cs:
Click around the sample and note the debug output. This should show for example that the data context of the button does not change when the button is moved to the content presenter, but it does change if the data context of the content presenter is updated afterwards. Also note that I still had to modify the template of the
TestControl
not to have theContent
in the visual tree.Workaround
When setting the
ContentPresenter.Content
, also set itsDataContext
to the same value as your content should have. This way it will inherit it normally.Environment
Nuget Package:
Nuget Package Version(s):
Affected platform(s):
IDE:
Relevant plugins:
Anything else we need to know?
The text was updated successfully, but these errors were encountered: