简体   繁体   English

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

[英]WPF C# How to animate HighlightBrush Color?

I'm trying to animate the selected item color of a ListView. 我正在尝试为ListView的选定项目颜色设置动画。

I can access this "property" through this code: 我可以通过以下代码访问此“属性”:

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

How can I animate the color of this "property"? 如何为该“属性”的颜色设置动画?

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

Many thanks! 非常感谢!

So here is the SampleStyle ;-) 这是SampleStyle ;-)

It uses a Template for the ListViewItem in which you can add a Stoyboard with a ColorAnimation in the Trigger Enter/Exit Actions for the IsSelected-Property! 它为ListViewItem使用模板,您可以在其中为IsSelected-Property的触发器输入/退出操作添加带有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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM