TemplateBinding from a Style DataTrigger In ControlTemplate

删除回忆录丶 提交于 2019-12-05 07:22:24

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!