开发者

Tri state image button cant get both mouse over & pressed work together

开发者 https://www.devze.com 2023-04-09 23:32 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号