这个触发器有什么问题?
问题描述:
我正在学习WPF模板,我正在创建一个按钮。我正在使用触发器来更改'IsMouseOver'上椭圆的填充属性。当我将触发器直接设置为“填充”属性时,它可以工作。但是当我尝试引用特定的SolidColorBrush时,出现编译错误。这个触发器有什么问题?
这工作:
<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>
这将导致错误:
<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>
错误是:
Cannot find the Trigger target 'FillBrush'. (The target must appear before any Setters,Triggers, or Conditions that use it.)
任何人都可以解释为什么第二种情况下不能正常工作?谢谢。
答
而不是命名您使用椭圆 编辑刷,是啊,你知道这个: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>
什么东西有点比黑色更复杂? – Li3ro 2015-12-13 12:50:23