I have a Page page
in a Frame frame
, with frame.DataContext = "foo"
.
(page.Parent as Frame).DataContext
is "foo"
. okpage.DataContext
is null
(also forced with ClearValue). okpage.DataContext
is null
. but I expected "foo"!Why isn't the DataContext inherited? As far as I understand the Frame sandboxes the content. But I couldn't find any documentation of this behavior - can anyone point me to a place where this is mentioned?
To answer your question about documentation of this behavior: It's not Microsoft documentation, but I have a couple of WPF books that both mention this.
"Essential Windows Presentation Foundation" says: (pp. 160-161)
There are two interesting models for hosting navigable content: isolated hosting and integrated hosting.
With isolated hosting the content is not trusted and is run in a completely isolated (sandboxed) environment. This is how WPF content is hosted when running in the system Web browser as a XAML Browser Application. For navigation to another application or HTML content, this isolated hosting model is supported with a
Frame
object.Integrated hosting, in which we want the content to behave as part of our application, is not supported at all in the system. When
Frame
navigates to content within the application, we get an odd hybrid of isolated and integrated behavior.Frame
isolates its content from its style (and its parent's style), but not from the application's style. Events don't bubble from the content inFrame
; however, the objects are accessible from theContent
property (meaning that they aren't isolated in a security sense).For all these reasons,
Frame
is most useful when we're working with external content, but it can be carefully used for application content.
That's all it has to say -- nothing about property inheritance.
"Windows Presentation Foundation Unleashed says (p. 95):
The
Frame
control holds arbitrary content, just like all other content controls, but it isolates the content from the rest of the UI. For example, properties that would normally be inherited down the element tree stop when they reach theFrame
.