如何在标签中使用isPressed

本文关键字:isPressed 标签 | 更新日期: 2023-09-27 18:28:13

这是我代码的一部分,我已经在谷歌上搜索过了,但没有人知道我的意思。

 <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="30" Height="30" Content="" FontFamily="FontAwesome" HorizontalAlignment="Right" VerticalAlignment="Center">
                <Label.Style>
                    <Style TargetType="{x:Type Label}">
                        <Style.Triggers>
                            <Trigger Property="Label.IsMouseOver" Value="True">
                                <Setter Property="Label.Background" Value="Yellow" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Label.Style>
            </Label>

如何在标签中使用isPressed

在这种情况下,您可以尝试使用EventTrigger。If当然更长,但它是使用纯标准XAML(而不是任何自定义)的最简单方法:

<Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
       Width="30" Height="30" Content="Clgt?" FontFamily="FontAwesome" 
       HorizontalAlignment="Right" VerticalAlignment="Center">
     <Label.Style>
        <Style TargetType="{x:Type Label}">
            <Style.Triggers>
                <EventTrigger RoutedEvent="MouseLeftButtonDown">
                   <BeginStoryboard>
                      <Storyboard>
                          <ColorAnimation To="Yellow" Duration="0" 
                            Storyboard.TargetProperty="Background.Color"/>
                      </Storyboard>
                   </BeginStoryboard>
                </EventTrigger>
                <EventTrigger RoutedEvent="MouseLeftButtonUp">
                   <BeginStoryboard>
                      <Storyboard>
                          <ColorAnimation To="Transparent" Duration="0" 
                            Storyboard.TargetProperty="Background.Color"/>
                      </Storyboard>
                   </BeginStoryboard>                    
                </EventTrigger>
             </Style.Triggers>
         </Style>
      </Label.Style>
 </Label>

MouseLeftButtonUp像这样时,最好使用RemoveStoryboard操作:

<Style.Triggers>
    <EventTrigger RoutedEvent="MouseLeftButtonDown">
        <BeginStoryboard Name="bg">
           <Storyboard>
              <ColorAnimation To="Yellow" Duration="0" 
                              Storyboard.TargetProperty="Background.Color"/>
           </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
    <EventTrigger RoutedEvent="MouseLeftButtonUp">
         <RemoveStoryboard BeginStoryboardName="bg"/>
    </EventTrigger>
</Style.Triggers>