鼠标左键按下时WPF TabItem样式

时间:2016-07-24 20:11:22

标签: wpf tabitem multitrigger

我正在为WPF TabControl TabItem定义一个样式。 我想要显示一个轻微的"推下来"当按下鼠标(左键)并且未选择TabItem但我找不到TabItem时对Trigger的效果。

我尝试定义MultiTrigger IsMouseLeftButtonDownIsPressed属性,但都没有被识别。

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

如何在Trigger上按下鼠标左键时设置一个TabItem

修改

我试过了EventTrigger,但我看不到下面的效果

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

可以用这种方式实现吗?               

1 个答案:

答案 0 :(得分:0)

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

注意

您可能必须在那些Triggers中完全设置突出显示逻辑,因为每次都会覆盖值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>