鼠标悬停时不会触发动画

时间:2014-03-05 10:29:06

标签: wpf xaml

我有小问题,一切正常,除非我进入鼠标时,当我离开背景更改时,但是当我将其设置为不会从白色变为我的颜色时。

<!-- 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 个答案:

答案 0 :(得分: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>