Another solution is to use the Content as in the first example, but add an explicit Data Context property to it before using any binding, something like this: In this case, though, you specify the Data Context as an ugly binding and, worst of all, you cannot set it via the Content Control, but you need to access the actual content.
Perhaps another solution, one that would involve a custom Data Template Selector on the Content Control would work, but right now I have no perfectly satisfactory solution.
Most of the resources on the web specify that Content Presenter supports a special property called Content Source, with a default value of “Content“, which makes it easy for it to automatically set the values of these properties used to render content: Basically, the property specifies the string prefix used to bind to properties of the parent.
If you change the value of the Content Source property to something else, like “Header“, the Content Presenter’s properties would auto-bind to bind to these properties of the control you are templating using Control Template: Why is it important? Bindings are resolved relatived to the value of the Data Context property.
I have a User List user control which has User List View and a User Detail user control which has a User Detail View class.
User List View displays a list of users which is fetched from the database.
The problem is that the view does not reflect the state of the View Model.
The intended usage of Content Presenter is to set the Content to some binding to a data object, then control the element tree via the Content Template property.I have navigation set up with Main that has a Content Control.Content property is bound to an object property in Main View Model.Well, if you used Content Presenters in your User Control, you would see this, instead: You would also see strange binding errors in the console System. Data Error: 40 : Binding Expression path error: 'Content Two Template' property not found on 'object' ''String' (Hash Code=-1506748533)'.Binding Expression: Path=Content Two Template; Data Item='String' (Hash Code=-1506748533); target element is 'Content Presenter' (Name=''); target property is 'Content Template' (type 'Data Template') System. Data Error: 40 : Binding Expression path error: 'Content Two Template Selector' property not found on 'object' ''String' (Hash Code=-1506748533)'.So I have a template containing a Text Box, and I have a View Model object containing a Name property, and I bind the Text property of the Text Box to the Name property of the View Model, and ... I've checked the visual tree with Snoop, and it confirms that the Data Context of the Text Box is the View Model object. And if I modify the (empty) Text in the Text Box, the Name property in the View Model does not change.