在ControlTemplate中,从样式DataTrigger的TemplateBinding

7
在以下XAML中,我使用一个带边框的矩形作为ToggleButton的模板。 我想要BorderBrush的颜色不同以反映ToggleButton.IsChecked的变化值。不幸的是,在DataTrigger中使用TemplateBinding的尝试失败了。我需要做什么才能实现这个效果?
<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <ControlTemplate x:Key="ButtonAsSwatchTemplate">
            <Border BorderThickness="1">
                <Border.Style>
                    <Style>
                        <Setter Property="BorderBrush" Value="Gainsboro" /> 
                        <Style.Triggers>
                            <!-- TemplateBinding doesn't work.-->
                            <DataTrigger                              
                                 Binding={TemplateBinding Property=IsChecked}    
                                 Value="True">
                                <Setter Property="BorderBrush" Value="Black" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Border.Style>
                <Rectangle Fill="{TemplateBinding Property=Background}"
                           Width="15" Height="15" />
            </Border>
        </ControlTemplate>
    </StackPanel.Resources>
    <ToggleButton Template="{StaticResource ButtonAsSwatchTemplate}"
                  HorizontalAlignment="Center" VerticalAlignment="Center"
                  Margin="2" Background="Red" />
</StackPanel>

编辑

当我构建并重新加载设计师时,我遇到了以下错误:

错误1:属性“Binding”不支持“TemplateBindingExpression”类型的值。

解决方案

<StackPanel Orientation="Horizontal">
    <StackPanel.Resources>
        <ControlTemplate x:Key="ButtonAsSwatchTemplate">    
            <Border x:Name="SwatchBorder" BorderThickness="1">
                <Rectangle Fill="{TemplateBinding Property=Background}" Width="15" Height="15" />
            </Border>
        <ControlTemplate.Triggers>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
                <Setter TargetName="SwatchBorder" Property="BorderBrush" Value="Yellow" />
            </Trigger>
        </ControlTemplate.Triggers>
            </ControlTemplate>
        </StackPanel.Resources>
    <RadioButton Template="{StaticResource ButtonAsSwatchTemplate}"
        GroupName="CropGuidesColourRadioButtonGroup"
        IsChecked="{Binding Checked}" Margin="2" Background="Red" />
    <RadioButton Template="{StaticResource ButtonAsSwatchTemplate}"
        GroupName="CropGuidesColourRadioButtonGroup" 
        IsChecked="{Binding Checked}" Margin="2" Background="Black" />
    ...
</StackPanel>
2个回答

9
您可以在ControlTemplate中使用触发器,例如:
<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>

1
谢谢您。我一直在尝试在ControlTemplate触发器中进行TemplateBindings,但没有意识到我可以从ControlTemplate访问控件的依赖属性。 - William Thomas Waller

0
我看到的一个问题是,您正在将BorderBrush设置为Border的本地属性值。这将始终覆盖样式应用的值。尝试移除显式的BorderBrush属性,看看是否可以解决问题。

边框 BorderBrush="Gainsboro" BorderThickness="1"

依赖属性值继承


好的,我已经从我的代码中删除了那部分,并且更新了我问题中的示例,但是我仍然得到了错误,我已经在上面作为编辑添加了。 - Grokodile

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接