Home > Software design >  How to bind a property to another property in the same control in a custom template
How to bind a property to another property in the same control in a custom template

Time:09-26

I have a custom style along with a custom template for a button . I want to bind the borderbrush property to the foreground so that when i change the foreground the borderbrush is also changed with the same color.

style:

   <Style TargetType="Button" x:Key="LightbuttonStyle">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button" >
                <Border BorderBrush="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Foreground}" Margin="5" CornerRadius="7" Background="{TemplateBinding Background}" BorderThickness="5" >
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Black"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

button:

<Button Height="68" Width="290" FontSize="30" 
 Background="Transparent" Content="Hello"  
Style="{StaticResource LightbuttonStyle}" FontWeight="Bold" Foreground="#3b2143"
                ></Button>

The binding i tried to do above in the style only worked without a custom template

CodePudding user response:

use TempateBinding like you do for Background:

BorderBrush="{TempateBinding Foreground}"
  • Related