WPF 对列表框项背景颜色进行动画处理
本文关键字:动画 处理 颜色 背景 列表 WPF | 更新日期: 2023-09-27 17:56:32
我的代码接近完成我想要的,但我需要一些帮助。当我将鼠标悬停在单个列表框项目上时,我想从无背景色淡入 .5 不透明度橙色。这是我目前拥有的:
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Control.Background" Value="Orange" />
<Setter Property="Control.BorderBrush" Value="SteelBlue" />
<Setter Property="Control.BorderThickness" Value="1" />
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity" To=".5"
Duration="0:0:0.4"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetProperty="Opacity" To="0"
Duration="0:0:0"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
但这会导致单个 WHOLE 列表框项在鼠标悬停时显示 .5 不透明度橙色,然后在鼠标离开时完全消失。因此,存在两个问题:如何仅对每个列表框项的背景颜色属性进行动画处理,以及如何对动画进行排序以使其正常工作?
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Margin" Value="1,2,1,1"/>
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="Background" Value="White" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border Background="{TemplateBinding Background}" />
<Border Background="LightGray" Margin="0,0" Opacity="0.5">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<!--<Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" />-->
<Border Margin="2,1,2,0" Grid.Row="0">
<Border.Background >
<LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" >
</LinearGradientBrush>
</Border.Background>
</Border>
</Grid>
</Border>
<ContentPresenter Margin="0,5" />
</Grid>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True" />
<Condition Property="IsSelected" Value="False"/>
</MultiTrigger.Conditions>
<!--<Setter Property="Background" Value="#CCCBAF00" />
<Setter Property="Opacity" Value="0.8" />-->
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8">
<GradientStop Color="#F54F00" Offset="0" />
<GradientStop Color="#F54F00" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</MultiTrigger>
<Trigger Property="IsSelected" Value="True">
<!--<Setter Property="Background" Value="#CCCB6400" />
<Setter Property="Opacity" Value="0.8" />-->
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint=".5,0" EndPoint="0.5,1" Opacity="0.8">
<GradientStop Color="#CCCD8B4C" Offset="0" />
<GradientStop Color="#CCCB6400" Offset="1" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<ListBox Margin="20" ItemContainerStyle="{StaticResource ListboxItemStyle}" >
<ListBoxItem >New York</ListBoxItem>
<ListBoxItem>Los Angeles</ListBoxItem>
<ListBoxItem>Paris</ListBoxItem>
<ListBoxItem>Zurich</ListBoxItem>
</ListBox>
</Grid>
</Window>
如果我把它放在ListBox.Resources
而不是ItemContainerStyle
,你的 XAML 就可以工作
<ListBox>
<ListBox.Resources>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="HorizontalContentAlignment"
Value="Stretch" />
<Style.Triggers>
<Trigger Property="IsMouseOver"
Value="True">
<Setter Property="Control.Background"
Value="Orange" />
<Setter Property="Control.BorderBrush"
Value="SteelBlue" />
<Setter Property="Control.BorderThickness"
Value="1" />
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To=".5"
Duration="0:0:0.4" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="10"
Duration="0:0:0" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
<ListBoxItem>AAA</ListBoxItem>
<ListBoxItem>BBB</ListBoxItem>
<ListBoxItem>CCC</ListBoxItem>
</ListBox>