Search This Blog

Monday, November 4, 2013

ControlTemplate WPF Example

WPF > Controls  > Templates > ControlTemplate

ControlTemplate defines the visual elements tree that is applied as the control template for a control.
ContentPresenter it is a placeholder for any XAML content and it can be used to insert content at runtime.

Example: Create Ellipse Button

<Window x:Class="WPF_Tutorial.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1">

    <Window.Resources>
        <ControlTemplate TargetType="Button" x:Key="ctButton" >
            <Grid>
                <Ellipse Fill="{ TemplateBinding Background }" Height="{TemplateBinding Height }">
                </Ellipse>
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" Content="{TemplateBinding Button.Content}" />
            </Grid>
        </ControlTemplate>
    </Window.Resources>

    <Grid>
        <Button Content="Ellipse Button" HorizontalAlignment="Left" VerticalAlignment="Top"
                Background="BlueViolet"
                Width="100" Height="100" Template="{ StaticResource ctButton}" Foreground="#FFF3E2E2">
        </Button>
    </Grid>
</Window>