ColorAnimation in MultiDataTrigger's EnterActions not triggering when item is added to ListView

1.1k views Asked by At

I have a ListView with AlternationCount set to 2. I have a ListViewItem style that currently sets the background color of the ListViewItem to alternating colors, and I would like to add a third trigger which triggers a ColorAnimation to animate the color between Red and White when a property of the bound ViewModel is true (in this case a property called "Locked").

I came across this post, and tried the xaml at the bottom:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/e7897cbd-71d9-45e6-9b17-0cd5bde5859f

But, the animation doesn't appear to trigger for me until I mouse over or select the item, then select or mouse over a different item. I get the alternating colors from the first two MultiDataTriggers, but the animation doesn't trigger when the item is added to the collection. Here is my XAML:

    <Style x:Key="alternatingListViewItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="White" />
        <Style.Triggers>
            <MultiDataTrigger >
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="0" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ListViewItem.IsSelected)}" Value="False" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Panel.IsMouseOver)}" Value="False" />
                    <Condition Binding="{Binding Locked}" Value="False" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" Value="DarkGray" />
                </MultiDataTrigger>
            <MultiDataTrigger >
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ItemsControl.AlternationIndex)}" Value="1" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ListViewItem.IsSelected)}" Value="False" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Panel.IsMouseOver)}" Value="False" />
                    <Condition Binding="{Binding Locked}" Value="False" />
                </MultiDataTrigger.Conditions>
                <Setter Property="Background" Value="SlateGray" />
                </MultiDataTrigger>
            <MultiDataTrigger>
                <MultiDataTrigger.Conditions>
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(ListViewItem.IsSelected)}" 
                                         Value="False" />
                    <Condition Binding="{Binding RelativeSource={RelativeSource Self}, Path=(Panel.IsMouseOver)}" 
                                         Value="False" />
                    <Condition Binding="{Binding Locked}" 
                                         Value="True" />
                </MultiDataTrigger.Conditions>
                <MultiDataTrigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <ColorAnimation
                                Storyboard.TargetProperty="(Control.Background).(SolidColorBrush.Color)"
                                From="Red" To="White" Duration="0:0:0.2"
                                AutoReverse="True" RepeatBehavior="Forever" />
                        </Storyboard>
                    </BeginStoryboard>
                </MultiDataTrigger.EnterActions>
            </MultiDataTrigger>
        </Style.Triggers>
    </Style>
1

There are 1 answers

0
paparazzo On BEST ANSWER

I don't have the specific syntax for you example but you might try putting the triggers directly on the ListView (not in resources) and use ListView.ItemContainerStyle rather than Syle. I could not make the simple code below work in Resouces but when I moved it directly to the ListView it worked. I probably had a syntax error when it was in resources e.g.

    <ListView AlternationCount="2"
              ItemsSource="{Binding Path=...}">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <!-- setting up triggers for alternate background colors -->
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="Gainsboro"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="2">
                        <Setter Property="Background" Value="White"></Setter>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
    <ListView.View>