WPF C#如何为HighlightBrush颜色设置动画?

我正在尝试为ListView的所选项颜色设置动画.

我可以通过以下代码访问此“属性”:

<Style.Resources>
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Blue">
</Style.Resources>

如何设置此“属性”的颜色动画?

 <Storyboard x:Key="MyStoryboard">
     <ColorAnimation Storyboard.TargetName="MyList" 
                     Storyboard.TargetProperty="{x:Static SystemColors.HighlightBrushKey}"    // compilation error
                     To="Gray" Duration="0:0:1" />
 </Storyboard>

非常感谢!

最佳答案 所以这是SampleStyle 😉

它使用ListViewItem的模板,您可以在IsSelected-Property的Trigger Enter / Exit操作中添加带有ColorAnimation的Stoyboard!

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border x:Name="Bd" Background="{TemplateBinding Background}" SnapsToDevicePixels="true">
                    <Grid>
                        <GridViewRowPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                        <ContentPresenter x:Name="contentPresenter" Visibility="Collapsed" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="GridView.ColumnCollection" Value="{x:Null}">
                        <Setter TargetName="contentPresenter" Property="Visibility" Value="Visible"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="true">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                                    From="Red" To="Blue" Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>
                        <Trigger.ExitActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)"
                                                    From="Blue" To="Transparent" Duration="0:0:1" />
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.ExitActions>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
点赞