Tri state image button cant get both mouse over & pressed work together
Hi I'm trying to create a three state button using three images.
I cannot get both mouse over and pressed triggers work together. In the code below, mouse over works fine but the pressed image does not show when the button is pressed.
<Button x:Name="button"/>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image x:Name="Normal" Source="run.png" Visibility="Visible"/>
<Image x:Name="Pressed" Source="pressed.png" Visibility="Hidden"/>
<Image x:Name="Over" Source="over.png" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Ove开发者_JAVA百科r" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Normal" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate></Button.Template></Button>
Any ideas?
Triggers are evaluated in the order they appear in the XAML, so I think you most probably just need to re-order them as below:
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Normal" Property="Visibility" Value="Visible"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Over" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
</Trigger>
</ControlTemplate.Triggers>
This way the IsPressed
settings will override the IsMouseOver
equivalents.
精彩评论