Guilherme
Guilherme

Reputation: 5341

WPF C# How to animate HighlightBrush Color?

I'm trying to animate the selected item color of a 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!

Upvotes: 4

Views: 1011

Answers (1)

makim
makim

Reputation: 3284

So here is the 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!

<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>

Upvotes: 2

Related Questions