2014-03-05 194 views
0

我有一個小問題,除了當我輸入鼠標時,當我離開背景更改,但是當我將它設置爲不從白色更改爲我的顏色時,所有內容都可以正常工作。在鼠標懸停不觸發動畫

<!-- ComboBox: Item Triggers --> 
    <Style TargetType="{x:Type ComboBoxItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ComboBoxItem}"> 

        <Border Name="Border" CornerRadius="0" BorderThickness="0" Focusable="False" BorderBrush="Transparent" Background="White"> 
         <Grid VerticalAlignment="Center" HorizontalAlignment="Center"> 
          <ContentPresenter VerticalAlignment="Center" /> 
         </Grid> 
        </Border> 

        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Foreground" Value="White" /> 
         </Trigger> 
         <EventTrigger RoutedEvent="MouseEnter"> 
          <BeginStoryboard> 
           <Storyboard> 
            <ColorAnimation From="White" To="#52b0ca" Duration="0:0:0.5" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger> 
         <EventTrigger RoutedEvent="MouseLeave"> 
          <BeginStoryboard> 
           <Storyboard> 
            <ColorAnimation From="#52b0ca" To="White" Duration="0:0:0.5" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/> 
           </Storyboard> 
          </BeginStoryboard> 
         </EventTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

回答

1

看上去就像是處理MouseEnter事件,所以我會建議使用TriggerBase.EnterActionsTriggerBase.ExitActions來代替:

<ControlTemplate TargetType="{x:Type ComboBoxItem}"> 
    <Border Name="Border" CornerRadius="0" BorderThickness="0" Focusable="False" BorderBrush="Transparent" Background="White"> 
     <Grid VerticalAlignment="Center" HorizontalAlignment="Center"> 
     <ContentPresenter VerticalAlignment="Center" /> 
     </Grid> 
    </Border> 
    <ControlTemplate.Triggers> 
     <Trigger Property="IsMouseOver" Value="true"> 
     <Setter Property="Foreground" Value="White" /> 
     <Trigger.EnterActions> 
      <BeginStoryboard> 
       <Storyboard> 
        <ColorAnimation From="White" To="#52b0ca" Duration="0:0:0.5" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/> 
       </Storyboard> 
      </BeginStoryboard> 
     </Trigger.EnterActions> 
     <Trigger.ExitActions> 
      <BeginStoryboard> 
       <Storyboard> 
        <ColorAnimation From="#52b0ca" To="White" Duration="0:0:0.5" Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"/> 
       </Storyboard> 
      </BeginStoryboard> 
     </Trigger.ExitActions> 
     </Trigger> 
    </ControlTemplate.Triggers> 
</ControlTemplate> 
相關問題