Search code examples
wpfsilverlightcontroltemplate

Please teach me how to use ControlTemplate properly?


Ok, I think I am pretty noob on using ControlTemplate lol...

All I want is to template my every UserControl so every UserControl will have Delete, Save, Cancel buttons.

So now I am writting some test...... Try to make a border around the everything...

In App.xaml

<Application.Resources>
    <ControlTemplate x:Key="DeleteSaveCancelTemplate">
        <Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
            <Border BorderThickness="5" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Black"> 
                <ContentPresenter/>
            </Border>
        </Grid>
    </ControlTemplate>
</Application.Resources>

Then in UserControl:

<UserControl ...  Height="150" Width="300"  Template="{StaticResource DeleteSaveCancelTemplate}" >

But all I see is just ...black... what did I do wrong? I thought I should use ContentPresenter to shows the content?


Solution

  • So... I found the problem lol From this page The ControlPresenter doens't bind to Content by default... I need to write

       <ContentPresenter Content="{TemplateBinding Property=ContentControl.Content}" />
    

    Unless you have specify the TargetType, then you it will bind by default.