Study your flashcards anywhere!

Download the official Cram app for free >

  • Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

How to study your flashcards.

Right/Left arrow keys: Navigate between flashcards.right arrow keyleft arrow key

Up/Down arrow keys: Flip the card between the front and back.down keyup key

H key: Show hint (3rd side).h key

A key: Read text to speech.a key

image

Play button

image

Play button

image

Progress

1/9

Click to flip

9 Cards in this Set

  • Front
  • Back

ContentControl vs ContentPresenter

The most significant difference is that ContentPresenter has the ContentSource property while ContentControl hasn’t.
ContentControl vs ContentPresenter example
For instance if you have a UserControl “MyControl” that defines a dependency property called “MyProperty”, you can use the value of MyControl.MyProperty in the MyControl.Template in this way:

1
2
3
4
5
<ControlTemplate TargetType="MyControl">
<StackPanel>
<ContentPresenter ContentSource="MyProperty" />
</StackPanel>
</ControlTempalte>
Instead using the ContentControl you could write the same template in this way:

1
2
3
4
5
<ControlTemplate TargetType="MyControl">
<StackPanel>
<ContentControl Content="{TemplateBinding MyProperty}" />
</StackPanel>
</ControlTempalte>
ContentControl vs ContentPresenter explanation
In fact the ContentControl has a template that uses a ContentPresenter to show it’s own Content property using the ContentSource. The ContentPresenter is a light-weight component that is supposed to be used in a template as a simple place-holder for the Content property. The default value for the ContentPresenter.ContentSource is “Content”, so you just need to add an empty ContentPresenter in a template to let be the place-holder of the Content property of the template parent instance.
ContentPresenter
In WPF Content Presenter is a control that displays a single piece of content.

CONTENT PRESENTER: Content Presenter in WPF is used inside control templates, as well as inside the root application markup. The concept of ContentPresenter is quite simple – it is a placeholder for any XAML content and it can be used to insert content at runtime. Or we can say that ContentPresenter is a class that will automatically take the content of the ContentControl and display it, when placed into a ContentControl's ControlTemplate.

A content presenter is used via the ContentPresenter element:
<ContentPresenter></ContentPresenter>

Syntax:
<ContentPresenter Name="MyContent">
<ContentPresenter.Content>
<Button>Click Me</Button>
</ContentPresenter.Content>
</ContentPresenter>
ContentControl, ContentPresenter and ContentTemplate.
1, ContentControl is one control, we could use it directly, just assign one child control in it.

2, ContentPresenter is similar with the ContentControl, but we cannot use it drectly, we could us eit in the template, to presenta one content property. We could conitune to set this control.Content property, and the child element is set on the ContentPresenter.Content in the cotnrol's Visual Tree actually.

3, ControlTempalte is used to design the control remplate which control presented. It is different with the above, we could design many things in it, and it has its Trigger. The controls has the Template property we could assign the ControlTemplate on it.

ContentControl is control which is supports only one child.

ContentPresenter shows the content whatever we assigned in the Content property of ContentControl.

ContentTemplate defines how the Content should be displayed.
ContentPresenter vs ContentControl more
The major difference can be summarized this way: The ContentPresenter has a property the ContentControl doesn’t have: ContentSource!

The ContentPresenter.ContentSource property maps the content of the ContentPresenter to a parent template instance property (done during the automatic aliasing step).
This means that if a control « MyControl » has a dependancy property « MyProperty » defined, you can put the value of MyControl.MyProperty in your ControlTemplate this way:

view plaincopy to clipboardprint?
<ControlTemplate TargetType="MyControl">
<Grid>
...
<ContentPresenter ContentSource="MyProperty" />
...
</Grid>
</ControlTempalte>
This is a sort of optimized alternative to (not always true!):

view plaincopy to clipboardprint?
<<ontrolTemplate TargetType="MyControl">
<Grid>
...
<ContentControl Content="{TemplateBinding MyProperty}" />
...
</Grid>
</ControlTempalte>
ContentPresenter vs ContentControl more cd
(By the way, the ControlControl uses a ContentPresenter by default to display its Content property...)

The default value of the ContentPresenter.ContentSource is "Content": if you have a control that have a Content property, you put its value by just adding a ContentPresenter in you ControlTemplate.

The ContentPresenter has been designed to be a kind of light-weight Content place holder inside a ControlTemplate.
For the all other cases, the ContentControl should be used (Data/Form separation and so on...).

If you still use a ContentPresenter for other purposes do not forget to reset the ContentSource property: ContentSource=""
Control Template
Control Template
This template specifies the appearance of a Control; if a control does not have a Control Template, the Control will not appear in your application.
For Example - When you will add the template defines as below to your application as a resource then all the buttons in the application will appear as ellipses but will still function as buttons.

<Style TargetType="Button">
<!--Set to true to not get any properties from the themes-->
<Setter Property="OverridesDefaultStyle" Value="True"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<Ellipse Fill="{TemplateBinding Background}"/>
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Data Template
Data Template
This template specifies a group of characteristics for how data should be displayed. This template is particularly useful when you are binding an ItemsControl such as a ListBox to an entire collection.
For Example – The Template defined as below, is used to display the items of a ListBox. The data template contains TextBlock controls that bind to the FirstName, LastName, and Address properties.

>Grid>
>Grid.Resources>
>src:Customers x:Key="customers"/>
>/Grid.Resources>
>ListBox ItemsSource="{StaticResource customers}" Width="350" Margin="0,5,0,10">
>ListBox.ItemTemplate>
>DataTemplate>
>StackPanel Orientation="Horizontal">
>TextBlock Padding="5,0,5,0"
Text="{Binding FirstName}" />
>TextBlock Text="{Binding LastName}" />
>TextBlock Text=", " />
>TextBlock Text="{Binding Address}" />
>/StackPanel>
>/DataTemplate>
>/ListBox.ItemTemplate>
>/ListBox>
>/Grid>