将项目添加到ListView时,MultiDataTrigger的EnterActions中的ColorAnimation不会触发

时间:2011-03-02 21:21:45

标签: c# .net wpf windows multidatatrigger

我有一个ListView,AlternationCount设置为2.我有一个ListViewItem样式,当前将ListViewItem的背景颜色设置为交替颜色,我想添加第三个触发器,触发ColorAnimation动画红色之间的颜色当绑定的ViewModel的属性为true时(在这种情况下称为“已锁定”的属性)。

我遇到过这篇文章,并尝试了底部的xaml:

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

但是,在我鼠标悬停或选择项目,然后选择或鼠标悬停在其他项目上之前,动画似乎不会触发我。我从前两个MultiDataTriggers获得交替颜色,但是当项目添加到集合时动画不会触发。这是我的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 个答案:

答案 0 :(得分:2)

我没有针对您的具体语法示例,但您可能会尝试将触发器直接放在ListView上(而不是在资源中)并使用ListView.ItemContainerStyle而不是Syle。我无法使下面的简单代码在Resouces中工作,但是当我将它直接移动到ListView时它工作了。我在资源中遇到语法错误,例如

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