三态图像按钮无法同时将鼠标悬停在上面和压在一起工作

发布于 2024-12-07 15:24:14 字数 1635 浏览 0 评论 0原文

您好,我正在尝试使用三个图像创建一个三状态按钮。

我无法让鼠标悬停和按下的触发器同时工作。在下面的代码中,鼠标悬停工作正常,但按下按钮时不会显示按下的图像。

<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="Over" 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>

有什么想法吗?

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="Over" 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-12-14 15:24:14

触发器按照它们在 XAML 中出现的顺序进行评估,因此我认为您很可能只需要按如下方式重新排序:

 <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>

这样 IsPressed 设置将覆盖 IsMouseOver 等价物。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文