I'm learning about WPF templates and I'm creating one for a button. I'm using a trigger to change the Fill property of the Ellipse on 'IsMouseOver'. When I set the trigger to the 'Fill' property directly, it works. But when I try to reference a specific SolidColorBrush, I get a compile error.
This works:
<ControlTemplate x:Key="GhostButtonTemplate" TargetType="Button">
开发者_运维知识库 <Grid>
<Ellipse Name="Border" Stroke="DarkGray" Fill="Gray">
</Ellipse>
<ContentPresenter ... />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Fill" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
This causes error:
<ControlTemplate x:Key="GhostButtonTemplate" TargetType="Button">
<Grid>
<Ellipse Name="Border" Stroke="DarkGray">
<Ellipse.Fill>
<SolidColorBrush Color="Gray" x:Name="FillBrush"/>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter ... />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="FillBrush" Property="Color" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
Error is:
Cannot find the Trigger target 'FillBrush'. (The target must appear before any Setters,Triggers, or Conditions that use it.)
Can anyone explain why the second case doesn't work? Thanks.
Rather than naming the brush you use the Ellipse Edit, yeah you know this :P
<ControlTemplate x:Key="GhostButtonTemplate" TargetType="Button">
<Grid>
<Ellipse Name="Border" Stroke="DarkGray">
<Ellipse.Fill>
<SolidColorBrush Color="Gray" x:Name="FillBrush"/>
</Ellipse.Fill>
</Ellipse>
<ContentPresenter ... />
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Fill" Value="Black"/>
</Trigger>
</ControlTemplate.Triggers>
精彩评论