I'm using an application framework that sets up a HeaderedContentControl and places my usercontrol in the content section.
Is there any way I can change the Header template from within the Content section?
No, you cannot set any property or modify the HeaderTemplate of the parent HeaderedContentControl in the Content "section" in the XAML markup.
What you can to is to programmatically handle the Loaded event for the content, i.e. the UserControl in this case, and get a reference to the parent HeaderedContentControl using the VisualTreeHelper class.
Related
In my WPF app, I'm using several ContentPresenters with a special MarkupExtension that requires access to the ContentPresenter's ContentTemplate property.
The MarkupExtension works very well, except that I just found out that if a ContentPresetner uses a ContentTemplateSelector, it doesn't set its own ContentPresenter property: rather, both the ContentPresenter and the result of the ContentTemplateSelector get saved to a private variable of the ContentPresenter class, as can be seen here: link to .Net source code for ContentPresenter.
I figured out that I can call ContentTemplateSelector.SelectTemplate() again and get the template, or keep a dictionary of selected templates inside the ContentTemplateSelector so that I can fetch the template that was geneatedfor each element, but is there a better way to do this?
I have a UserControl without Content, because the control which should be shown inside of the UserControl is created at runtime. I would like to solve this like follows, but don't know how to implement it:
Create a Control-variable in the ViewModel
Set it at runtime when the content is created
Bind a content property (inside the UserControl) to that variable
The problem is, that I don't know how to bind to the control-variable.
Why just not to use ContentControl instead of UserControl and provide Content in runtime by introducing a DataTemplateSelector which able to provide right DataTemplate in runtime?
You can encapsulate your Content-area controls in DataTemplates and select appropriate one in runtime.
I've like 'base' DataTemplate that contains TabControl with 3 tabs. In each tab I put empty ContentPresenter.
I want to write new DataTemplate for each derived type. In which, I want to populate the all 3 ContentPresenter.
How can I reffer to each ContentPresenter of the base template so I can put data inside?
I assume you have set content of each tabitem.
If the content objects have different types you just have to make a datatemplate for each of the types.
If I can replace it with a single TextBox (like on a button),
or I can add media element directly to Grid (whatever)...
What is ContentPresenter for? Is there some advantages?
You don't always need a ContentPresenter. It acts as a placeholder that will effectively host any content that you assign to its Content attribute. If you have an area on a given control/page that can hold dynamic content of an indeterminate type, a ContentPresenter is an effective way to hold the space.
It's also used quite a bit with templating, custom controls, etc. Odds are you won't actually use it until you start getting into some fairly advanced stuff.
One kind of cool thing you can do is have the Content attribute of the ContentPresenter bound to a DependencyProperty of type UserControl, and then if you set that DependencyProperty equal to any UserControl (like one that you new up in a ViewModel or something), it'll show up in that spot.
You typically use the ContentPresenter
in the ControlTemplate of a
ContentControl to specify where the
content is to be added. Every
ContentControl type has a
ContentPresenter in its default
ControlTemplate.
From MSDN; so basically it's a placeholder for content in a template.
It is used by ContentControl. Inside ContentControl's template, a ContentPresenter indicates as a placeholder where the actual content will be placed.
From MSDN,
Displays the content of a ContentControl.
In my Silverlight 4 application, I have a ContentControl with its ContentTemplate property bound to a property in the data context. That works fine. However, the content of the template once rendered has its DataContext set to null. I would like the content to inherit the same DataContext as set for the ContentControl. Is there a way to get this to happen?
The ContentControl's template has the ContentControl's Content property as a DataContext. So try
<ContentControl Content="{Binding}" />
if this is merely the current DataContext.
I found an alternate way to accomplish what was required. In my case, the template (not the content template) of the ContentControl was unimportant, so I made my DataTemplate objects into ControlTemplate objects instead and bound the Template property of the ContentControl instead of ContentTemplate. The data context was preserved if I did it this way.