在我阅读WPF期间,我遇到了一个问题,试图在模板中的Trigger上创建一个绑定,用于创建一个图像按钮。
<ControlTemplate x:Key="ToolbarButtonHover" TargetType="Button">
<Grid Name="backgroundGrid">
<Image Source="{DynamicResource ResourceKey=Img}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"></Image>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="backgroundGrid" Property="Background" Value="#007ACC" />
</Trigger>
<!--Error: The property 'Binding' was not found in type Trigger-->
<Trigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
<Setter TargetName="backgroundGrid" Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
导致错误The property 'Binding' was not found in type Trigger
,更具体地说是该行
<Trigger Binding="{ Path=IsMouseOver, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
生成它。
出现此错误的原因是什么?
答案 0 :(得分:10)
可能是因为Trigger类as you can see here上没有Binding属性。
您可能正在寻找DataTrigger。
<ControlTemplate x:Key="ToolbarButtonHover" TargetType="Button">
<Grid Name="backgroundGrid">
<Image Source="{DynamicResource ResourceKey=Img}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"></Image>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsPressed" Value="True">
<Setter TargetName="backgroundGrid" Property="Background" Value="#007ACC" />
</Trigger>
<!--Look below, DataTrigger -->
<DataTrigger Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource TemplatedParent}}" Value="True">
<Setter TargetName="backgroundGrid" Property="Background" Value="Red" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>