eye_am_groot
eye_am_groot

Reputation: 682

Cause New ListView Item Background to Flash When Added

As the title states, I am attempting to have a ListViewItembackground change colors when the item is loaded. I am able to get the opacity to change (my very striped down XAML):

<ListView Background="Black" ItemsSource="{Binding Somesource}" Drop="AddItem">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWay}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Grid x:Name="SignalGrid">
                            <!-- Grid Information -->
                        </Grid>
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
                                <Setter TargetName="SignalGrid" 
                                        Property="Background" 
                                        Value="Transparent"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
                                <Setter TargetName="SignalGrid" 
                                        Property="Background" Value="Blue"/>
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard AutoReverse="True" 
                                    RepeatBehavior="6x">
                            <DoubleAnimation Duration="0:0:0.3"
                                          Storyboard.TargetProperty="Opacity" 
                                          From="1.0" To="0.3"/>
                        </Storyboard>
                     </BeginStoryboard>
                 </EventTrigger>
             </Style.Triggers>
         </Style>
     </ListView.ItemContainerStyle>
</ListView>

This works fine, however is not the true feature I wamt. I tried to do the storyboard like:

<Storyboard AutoReverse="True" RepeatBehavior="6x">
    <ColorAnimation Duration="0:0:0.3" 
                    Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" To="Blue"/>
</Storyboard>

However this does nothing. I included the IsSelected DataTriggers because the basic idea is that I want to switch between IsSelected = true to IsSelected = false when the item is first added (i.e. the background toggles between blue and black). I am guessing that my issue is with Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" and I'm sure I'm missing something simple, but any help would be greatly appreciated.

EDIT I found the solution. In <!-- Grid Information --> I have a border, so I used this solution to solve my problem.

Upvotes: 0

Views: 361

Answers (3)

eye_am_groot
eye_am_groot

Reputation: 682

Just to close the question. As mentioned in the "EDIT," in <!-- Grid Information -->, I have a border, and changed it like so:

<Border>
    <Border.Background>
        <SolidColorBrush Color="Black" x:Name="RowBackground"/>
    </Border.Background>
</Border>

Then, still in the ControlTemplate:

<ControlTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=IsSelected}" Value="false">
        <Setter TargetName="SignalGrid" Property="Background" Value="Transparent"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding Path=IsSelected}" Value="true">
        <Setter TargetName="SignalGrid" Property="Background" Value="Blue"/>
    </DataTrigger>
    <EventTrigger RoutedEvent="Loaded">
        <BeginStoryboard>
            <Storyboard AutoReverse="True" RepeatBehavior="6x">
                <ColorAnimation Duration="0:0:0.5" Storyboard.TargetName="RowBackground" 
                                Storyboard.TargetProperty="Color" To="Blue"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</ControlTemplate.Triggers>

Upvotes: 0

Ilya Grigoryan
Ilya Grigoryan

Reputation: 259

<ListView Background="Black" ItemsSource="{Binding Somesource}" Drop="AddItem">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWay}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Grid x:Name="SignalGrid"  Background="{TemplateBinding Background}">
          <!-- Grid Information -->
    </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard AutoReverse="True" RepeatBehavior="6x">
    <ColorAnimation Duration="0:0:0.3" 
                    Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" To="Blue"/>
</Storyboard>
                     </BeginStoryboard>
                 </EventTrigger>
             </Style.Triggers>
         </Style>
     </ListView.ItemContainerStyle>
</ListView>

Upvotes: 1

mm8
mm8

Reputation: 169320

This should make the ListViewItem blink when loaded:

<ListView ItemsSource="{Binding Somesource}">
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=OneWay}" />
            <Setter Property="Background" Value="Transparent" />
            <Style.Triggers>
                <EventTrigger RoutedEvent="Loaded">
                    <BeginStoryboard>
                        <Storyboard AutoReverse="True" RepeatBehavior="6x">
                            <ColorAnimation Duration="0:0:0.3" 
                                            Storyboard.TargetProperty="(ListViewItem.Background).(SolidColorBrush.Color)" 
                                            To="Blue"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

Upvotes: 1

Related Questions