我有一个 ControlTemplate 在多个元素中使用相同的颜色。在某些触发器(例如 OnMouseOver)上,我想更改该颜色。据我所知,我必须为每个元素定义一个设置器来改变它的颜色。有没有办法在模板中引用所有包含的元素都可以访问的共享资源,并且可以通过触发器进行更改,所以我不必处理每个元素?
这是一个(编造的)示例:
<ControlTemplate x:Key="myTemplate" TargetType="{x:Type Button}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Ellipse Fill="red" Grid.Column="0"/>
<Ellipse Fill="red" Grid.Column="1"/>
<ContentPresenter Grid.ColumnSpan="2" VerticalAlignment="Center"/>
</Grid>
</ControlTemplate>
当控件被禁用时,我希望椭圆为灰色,而不是明确设置它们,例如我不想写
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="_ellipse1" Property="Fill" Value="Grey"/>
<Setter TargetName="_ellipse2" Property="Fill" Value="Grey"/>
</Trigger>
但是只用一个setter 设置两个椭圆的颜色。