鼠标左键按下时的WPF TabItem样式



我正在为WPF TabControl TabItem定义样式。我想在按下鼠标(左键)且未选择TabItem时,在TabItem上显示轻微的"按下"效果,但我找不到Trigger

我曾尝试定义具有IsMouseLeftButtonDownIsPressed属性的MultiTrigger,但两者都无法识别。

<Style TargetType="{x:Type TabItem}">
      <Setter Property="Template">
            <Setter.Value>
                  <ControlTemplate TargetType="TabItem">
                        <Border Name="bd" BorderThickness="1,1,0,0">
                            <Grid>
                                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center"
                                                  ContentSource="Header"/>    
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseLeftButtonDown" Value="True" /> <!--NOT RECOGNIZED-->
                                    <Condition Property="IsSelected" Value="False" />
                                </MultiTrigger.Conditions>
                                <Setter TargetName="bd" Property="Background" >
                                    <Setter.Value>
                                         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                              <GradientStop Color="Green" Offset="0"/>
                                              <GradientStop Color="Yellow" Offset="1"/>
                                         </LinearGradientBrush>
                                    </Setter.Value>
                                 </Setter>
                            </MultiTrigger>
                       </ControlTemplate.Triggers>
                 </ControlTemplate>
            </Setter.Value>
       </Setter>
 </Style>

当鼠标左键按下TabItem时,如何设置Trigger


编辑

我试过EventTrigger,但下面的没有任何效果

<EventTrigger RoutedEvent="UIElement.PreviewMouseLeftButtonDown">
     <BeginStoryboard>
           <Storyboard>
               <ColorAnimation Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" To="Green" Duration="0:0:3" />
           </Storyboard>
     <BeginStoryboard/>
<EventTrigger/>

用这种方式能实现吗?

此示例向您展示了如何使用EventTriggers

注意

您可能必须在这些Triggers中完全设置Highlighting Logic,因为每次值都会被覆盖,Trigger就会触发。

<EventTrigger RoutedEvent="PreviewMouseDown" >
    <EventTrigger.Actions>
        <BeginStoryboard>
            <Storyboard >
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="bd" Storyboard.TargetProperty="Background"  Duration="0:0:3" BeginTime="0" AutoReverse="True">
                    <ObjectAnimationUsingKeyFrames.KeyFrames>
                        <DiscreteObjectKeyFrame  KeyTime="0:0:0" Value="{x:Static Brushes.Green}" />
                    </ObjectAnimationUsingKeyFrames.KeyFrames>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>
<EventTrigger RoutedEvent="PreviewMouseUp" >
    <EventTrigger.Actions>
        <BeginStoryboard>
            <Storyboard >
                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="bd" Storyboard.TargetProperty="Background"  Duration="0:0:3" BeginTime="0" AutoReverse="True">
                    <ObjectAnimationUsingKeyFrames.KeyFrames>
                        <DiscreteObjectKeyFrame  KeyTime="0:0:0" Value="{x:Static Brushes.Transparent}" />
                    </ObjectAnimationUsingKeyFrames.KeyFrames>
                </ObjectAnimationUsingKeyFrames>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger.Actions>
</EventTrigger>

相关内容

  • 没有找到相关文章

最新更新