我有一个 WPF 应用程序,它有一个列表框,我正在尝试对其应用一些鼠标悬停效果。当我使用 simple Setter
s 更改鼠标悬停/选择的背景颜色时,一切正常,但我认为如果它在状态之间进行动画处理会更好看,所以我将Setter
s 切换为 enter/exit Storyboard
。最初一切都很好(鼠标悬停动画进出,选择动画进出),但是一旦选择了某些东西然后被取消选择,它就再也不会做鼠标悬停效果了。
这是显示问题的最小代码示例:
<Window x:Class="WpfTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="500" Width="500">
<Window.Resources>
<Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="border" BorderThickness="1" Height="50" Background="#000">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsSelected" Value="False" />
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border"
Storyboard.TargetProperty="Background.Color" To="#00F" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.3" Storyboard.TargetName="border"
Storyboard.TargetProperty="Background.Color" To="#008" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.15" Storyboard.TargetName="border"
Storyboard.TargetProperty="Background.Color" To="#F00" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Duration="0:0:0.3" Storyboard.TargetName="border"
Storyboard.TargetProperty="Background.Color" To="#800" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox x:Name="ConnectedDevicesListBox"
ItemContainerStyle="{DynamicResource ListboxItemStyle}" >
<ListItem/>
<ListItem/>
<ListItem/>
<ListItem/>
<ListItem/>
<ListItem/>
<ListItem/>
<ListItem/>
</ListBox>
</Grid>
</Window>
我已将退出动画淡化为非黑色,因此您可以看到它在IsSelected
退出情节提要后卡住了。
有任何想法吗?