1

我有一个将 AlternationCount 设置为 2 的 ListView。我有一个 ListViewItem 样式,该样式当前将 ListViewItem 的背景颜色设置为交替颜色,我想添加第三个触发器,该触发器触发 ColorAnimation 以在红色和白色之间为颜色设置动画绑定 ViewModel 的一个属性为真(在本例中为一个名为“Locked”的属性)。

我遇到了这篇文章,并尝试了底部的 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>
4

1 回答 1

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>
于 2011-04-28T15:28:00.187 回答