从 ControlTemplate 中的样式 DataTrigger 进行模板绑定
在下面的 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>
In the following XAML I'm using a Rectangle with a Border as the Template for a ToggleButton.
I want the BorderBrush to be a different colour to reflect the changing value of ToggleButton.IsChecked.
Unfortunately my attempt here of using a TemplateBinding in the DataTrigger doesn't work. What do I need to do instead?
<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>
EDIT
When I build and reload the designer I get the following error:
Error 1 Property 'Binding' does not support values of type 'TemplateBindingExpression'.
SOLUTION
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在 ControlTemplate 中使用触发器,例如
You could use a Trigger in the ControlTemplate, e.g.
我立即看到的一个问题是您将 BorderBrush 设置为 Border 上的本地属性值。这将始终覆盖样式应用的值。尝试删除显式的 BorderBrush 属性并查看是否有效。
边框
BorderBrush="Gainsboro"BorderThickness="1"依赖项属性值继承
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