Solved

Content Presenter (WPF)

Posted on 2014-04-08
3
220 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 22

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 22

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now