2016-07-24 52 views
2

我正在爲WPF定義一個樣式TabControlTabItem。 當鼠標(左按鈕)被按下並且TabItem未被選中但我找不到Trigger時,我想在TabItem上顯示輕微的「被按下」效果。WPF TabItem風格,當鼠標左鍵宕機

我試圖用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> 

如何設置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/> 

可以用它在某種程度上這種方式可以實現什麼影響?

+0

這是行不通的。如果你想聽MouseAction,你必須使用'EventTrigger'。 Eventriggers只會接受一個條件。因此你的'IsSelected = False'不會工作。解決方法可能是使用'IsMouseOver'而不是'IsMouseLeftButtonDown'。這將模擬懸停效果 – lokusking

+0

IsMouseOver在這裏不正確,因爲它給你一個懸停效果。我已經正確實施了。 – Nuts

+0

編輯了EventTrigger的帖子,但無法使其工作 – Nuts

回答

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>