TemplateBinding from a Style DataTrigger In ControlTemplate TemplateBinding from a Style DataTrigger In ControlTemplate wpf wpf

TemplateBinding from a Style DataTrigger In ControlTemplate


You could use a Trigger in the ControlTemplate, e.g.

<StackPanel Orientation="Horizontal">    <StackPanel.Resources>        <ControlTemplate x:Key="ButtonAsSwatchTemplate">            <Border x:Name="myBorder" BorderBrush="Gainsboro" BorderThickness="1">                <Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" />            </Border>            <ControlTemplate.Triggers>                <Trigger Property="ToggleButton.IsChecked" Value="True">                    <Setter TargetName="myBorder" Property="BorderBrush" Value="Black" />                </Trigger>            </ControlTemplate.Triggers>        </ControlTemplate>    </StackPanel.Resources>    <ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}"              HorizontalAlignment="Center" VerticalAlignment="Center"              Margin="2" Background="Red" /></StackPanel>


One problem I see right away is that you are setting BorderBrush as a local property value on Border. This will always override the value applied by the style. Try removing the explicit BorderBrush attribute and see if that works.

Border BorderBrush="Gainsboro" BorderThickness="1"

Dependency Property Value Inheritance