You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否可在ListView项的Style中添加背景色渐隐动画效果?

可以实现,全程使用XAML声明式写法即可,不需要任何后台代码,符合MVVM规范。

你需要将NormalStyle做如下修改即可覆盖你要求的两个触发场景:

<Style TargetType="ListViewItem">
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="Padding" Value="2"/>
    <Setter Property="MinHeight" Value="0"/>
    <Setter Property="MinWidth" Value="0"/>
    <Setter Property="FontSize" Value="10"/>
    <!-- 初始化背景为红色 -->
    <Setter Property="Background" Value="Red"/>
    <Style.Triggers>
        <!-- 场景1:新项添加到集合时,ListViewItem加载完成自动触发渐隐动画 -->
        <EventTrigger RoutedEvent="Loaded">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation 
                        Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                        From="Red" 
                        To="Transparent" 
                        Duration="0:0:5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
        <!-- 场景2:现有集合项更新时触发动画 -->
        <!-- 要求你的数据项T实现INotifyPropertyChanged接口,新增通知属性如ItemUpdated,每次项数据更新时将该属性设为True即可触发 -->
        <DataTrigger Binding="{Binding ItemUpdated}" Value="True">
            <BeginStoryboard>
                <Storyboard>
                    <ColorAnimation 
                        Storyboard.TargetProperty="Background.(SolidColorBrush.Color)"
                        From="Red" 
                        To="Transparent" 
                        Duration="0:0:5" />
                </Storyboard>
            </BeginStoryboard>
        </DataTrigger>
    </Style.Triggers>
</Style>

补充说明:

  • 动画直接作用于Style定义的Background属性,完全满足你的需求。如果需要最终渐隐到其他颜色,修改ColorAnimation的To值即可。
  • 如果你使用的是UWP环境,原生Style触发器支持有限,可以使用Microsoft.Xaml.Behaviors库的触发器替换,写法同样为纯XAML声明式,不需要编写后台逻辑。

内容的提问来源于stack exchange,提问作者vicNeo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 15:06:05