Link to home
Start Free TrialLog in
Avatar of VisionTeknology
VisionTeknologyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Content Presenter (WPF)

I am just looking through a sample piece of code

    <TabControl.ItemTemplate>
                    <DataTemplate>
                        <ContentPresenter Content="{Binding DisplayName}" />
                    </DataTemplate>
                </TabControl.ItemTemplate>

Why would they use a Content Presenter?
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Can't really say in this case since I don't know the rest of the code, but:
ContentPresenter is very handing when overriding templates.

For example, if you were overriding the template of a label, to put it in a grid or stackpanel or something, then the ContentPresenter will render all of the xaml markup in the original control.

In this case, you could possibly have a list where the DisplayName actually has xaml in it,
<Label><StackPanel><TextBlock>Foo</TextBlock><TextBlock>Bar</TextBlock></Label>

For something like this, the only way to get it to actually render the xaml properly is using the content presenter.   If you just bound to another element, you would get a text version of the markup.


Now, in this particular case, I would be very surprised if DisplayName had anything other than a string in it, in which case using ContentPresenter is definitely overkill, but might be just to accommodate future expansions.
Avatar of VisionTeknology

ASKER

The binding is a string, could you not use a <TextBlock>?
ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial