Solved

Content Presenter (WPF)

Posted on 2014-04-08
3
226 Views
Last Modified: 2014-06-17
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?
0
Comment
Question by:VisionTeknology
  • 2
3 Comments
 
LVL 23

Expert Comment

by:Snarf0001
ID: 39987395
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.
0
 

Author Comment

by:VisionTeknology
ID: 39988133
The binding is a string, could you not use a <TextBlock>?
0
 
LVL 23

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 39988461
Definitely, and would be lighter weight.  As I mentioned, it's probably just there for future expansion or something like that.
Textblock is 100% sufficient in this case.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question