0

Is it possible for the ControlTemplate to use a property from the control, that uses the template?

For example, I've got a button, that changes color to Red on MouseOver. But, I've also got a button, that looks exactly the same, except it changes to White, instead of Red. Would it be possible, that whatever Background value the Button has, that value is then used in the control template?

Currently, this is what my ControlTemplate looks like:

<ControlTemplate x:Key="CloseButtonTemplate" TargetType="{x:Type Button}">
        <Border>
            <Border.Style>
                <Style>
                    <Setter Property="Border.Background" Value="Transparent"/>
                    <Style.Triggers>
                        <Trigger Property="Border.IsMouseOver" Value="True">
                            <Setter Property="Border.Background" Value="#FFE53935" />
                            <Setter Property="Window.Cursor" Value="Hand" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBlock Foreground="#FFEEEEEE" HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" FontFamily="Calibri" />
        </Border>
</ControlTemplate>

What I'm trying to do, is have Border.Background set to whatever the Button Background value is. So if I have a <Button Background="Red" />, then the value of Border.Background is Red.

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Gregor Menih
  • 5,036
  • 14
  • 44
  • 66

2 Answers2

0

You should probably do this:

<Border x:Name="border" Background="{TemplateBinding Background}">

See TemplateBinding on MSDN

Piyush Parashar
  • 866
  • 8
  • 20
0

Yes It is possible for the ControlTemplate to use a property from the control, that uses the template.See this link for binding


   <Window.Resources>
    <ControlTemplate x:Key="CloseButtonTemplate" TargetType="{x:Type Button}">
        <Border BorderBrush="Black" BorderThickness="1"  Height="{TemplateBinding Height}" Width="{TemplateBinding Width}">
            <Border.Style>
                <Style>
                    <Setter Property="Border.Background" Value="Transparent"/>
                    <Style.Triggers>
                        <Trigger Property="Border.IsMouseOver" Value="True">
                            <Setter Property="Border.Background" Value="{Binding Path=Background,RelativeSource={RelativeSource TemplatedParent}}" />
                            <Setter Property="Window.Cursor" Value="Hand" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBlock Foreground="#FFEEEEEE" HorizontalAlignment="Center" VerticalAlignment="Center" Text="X" FontFamily="Calibri" />
        </Border>
    </ControlTemplate>
</Window.Resources>
<UniformGrid>
    <Button Background="Red" Template="{StaticResource CloseButtonTemplate}"  Height="30" Width="200"/>
    <Button Background="Green" Template="{StaticResource CloseButtonTemplate}"  Height="30" Width="200"/>
    <Button Background="Blue" Template="{StaticResource CloseButtonTemplate}"  Height="30" Width="200"/>
</UniformGrid>
Community
  • 1
  • 1
Heena
  • 8,450
  • 1
  • 22
  • 40