TorbenJ
TorbenJ

Reputation: 4582

Set padding of UserControl with assigned ControlTemplate?

I recently created an UserControl for my WrapPanel to visualize some data. I applied a Padding to it in order to get some space between each element.

The first version looked like this:

<UserControl x:Class="IFCS.EntityOverviewPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:l="clr-namespace:IFCS"
         mc:Ignorable="d"
         Padding="5, 5, 5, 5"
         d:DesignHeight="300" d:DesignWidth="300">
    <!-- Code -->
</UserControl>

Now I just applied a ControlTemplate to it which overrides my Padding setting.

The current version looks like this:

<UserControl x:Class="IFCS.EntityOverviewPanel"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:l="clr-namespace:IFCS"
         mc:Ignorable="d"
         d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <!-- Code -->
        </ControlTemplate>
    </UserControl.Template>
</UserControl>

I would like to apply Padding to my UserControl again but everything I tried didn't work. I tried to apply a Style using

<UserControl.Style>
    <Style TargetType="{x:Type UserControl}">
        <Setter Property="Padding" Value="5, 5, 5, 5"/>
    </Style>
</UserControl.Style>

but this didn't work. Setting the Padding in the "header" isn't working too.

Where do I have to set the Padding value in order to achieve the same result as in the first version?

Upvotes: 1

Views: 1910

Answers (1)

SHSE
SHSE

Reputation: 2433

<UserControl Padding="5,5,5,5">
    <UserControl.Template>
        <ControlTemplate TargetType="UserControl">
            <ContentPresenter Margin="{TemplateBinding Padding}" />
            <!-- Code -->
        </ControlTemplate>
    </UserControl.Template>

    <!-- Content -->
</UserControl>

Upvotes: 5

Related Questions