System.Windows.Controls Nam ...


.NET Framework Class Library for Silverlight
ContentPresenter Class

Displays the content of a ContentPresenter.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)
Syntax

Visual Basic (Declaration)
<ContentPropertyAttribute("Content", True)> _
Public Class ContentPresenter _
    Inherits FrameworkElement
Visual Basic (Usage)
Dim instance As ContentPresenter
C#
[ContentPropertyAttribute("Content", true)]
public class ContentPresenter : FrameworkElement
XAML Object Element Usage
<ContentPresenter .../>
Remarks

Typically, you use the ContentPresenter directly within the ControlTemplate of a ContentControl to mark where the content is to be added. The ContentPresenter should use a template binding to associate the ContentControl..::.Content property with the ContentPresenter..::.Content property. For more information, see TemplateBinding Markup Extension.

Examples

The following example creates a ControlTemplate for Button and uses a ContentPresenter to display the content of the Button. Although the Button looks like a rectangle with content, it still behaves like a Button. When the user clicks on a button, the Click event occurs. The button does not change its appearance when the user moves the mouse over it or clicks on it because the ControlTemplate does not contain any VisualState objects. For more information about VisualState objects, see Customizing the Appearance of an Existing Control by Using a ControlTemplate.

XAML
<StackPanel>
  <StackPanel.Resources>
    <Style x:Key="newButtonTemplate" TargetType="Button">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="Button">
            <Grid>
              <Rectangle Fill="{TemplateBinding Background}" />
              <ContentPresenter 
                HorizontalAlignment="{TemplateBinding HorizontalAlignment}" 
                VerticalAlignment="{TemplateBinding VerticalAlignment}" />
            </Grid>
          </ControlTemplate>

        </Setter.Value>
      </Setter>
    </Style>
  </StackPanel.Resources>

  <TextBlock Text="ContentPresenter Demonstration" Margin="0,20,10,20"
           FontFamily="Verdana" FontSize="18" FontWeight="Bold"
           Foreground="#FF5C9AC9" Grid.Row="0" />

  <Button Content="default button" 
          Width="200" Margin="7"
          Click="Button_Click"/>

  <Button Content="button with control template" 
          Style="{StaticResource newButtonTemplate}"
          Margin="7" Background="LightGray" Width="200"
          Click="Button_Click"/>

  <Button Content="another button with control template" 
          Style="{StaticResource newButtonTemplate}"
          Margin="7" Background="LightBlue" Width="200" 
          Click="Button_Click"/>

</StackPanel>

The following example shows the handler for the Click event.

Run this sample

XAML
private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("You clicked the button");
}
Inheritance Hierarchy

System..::.Object
  System.Windows..::.DependencyObject
    System.Windows..::.UIElement
      System.Windows..::.FrameworkElement
        System.Windows.Controls..::.ContentPresenter
          System.Windows.Controls..::.ScrollContentPresenter
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Tags :


Page view tracker