Solved

Content Presenter (WPF)

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 51
ASP.NET MVC 2 31
Help with adding DLL file in Windows project 20 28
Convert datetime to time string 10 20
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 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