WPF ListBox 中选中项目的动画效果

3

我正在尝试为应用程序中的所有列表框设置全局样式。以下是我使用的xaml代码。在这里,我尝试触发一个动画,但它不起作用。我只想在所选项目上进行动画。有什么帮助吗?

<Style TargetType="{x:Type ListView}">
    <Style.Setters>
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate x:Name="ListViewItemTemplate">
                    <TextBlock Text="{Binding}" Padding="0,0,5,5"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="ListViewItemBase.Selected">
                            <BeginStoryboard>
                                <Storyboard TargetProperty="Color">
                                    <ColorAnimation To="#FFFF0000" Duration="0:0:1" AutoReverse="true" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

工作版本:

<Style TargetType="{x:Type ListView}"> 
<Style.Setters> 
    <Setter Property="BorderThickness" Value="5" /> 
    <Setter Property="FontSize" Value="16" /> 
    <Setter Property="FontFamily" Value="Arial" /> 
    <Setter Property="ItemTemplate"> 
        <Setter.Value> 
            <DataTemplate x:Name="ListViewItemTemplate"> 
                <TextBlock Text="{Binding}" Padding="0,0,5,5"/> 
            </DataTemplate> 
        </Setter.Value> 
    </Setter> 
    <Setter Property="ItemContainerStyle"> 
        <Setter.Value> 
            <Style> 
                <Style.Triggers>
                    <Trigger Property="ListViewItem.IsSelected" Value="True"> 
                        <Trigger.EnterActions> 
                            <BeginStoryboard> 
                                <Storyboard Target="ListViewItem" TargetProperty="Background.Color"> 
                                    <ColorAnimation To="Red" Duration="0:0:0.5" AutoReverse="true" /> 
                                </Storyboard> 
                            </BeginStoryboard> 
                        </Trigger.EnterActions> 
                    </Trigger> 
                </Style.Triggers>
            </Style> 
        </Setter.Value> 
    </Setter> 
</Style.Setters> 

1个回答

5
为ListBox创建ItemContainerStyle并添加当ListBoxItem.IsSelected == True时的触发器。

1
不要设置Template属性,而是设置ItemContainerStyle属性。在其中创建一个带有Jobi所说的触发器的样式,然后在触发器激活时应用您的动画。 - Benny Jobigan
谢谢,我今天会尝试一下并告诉你 :) - Amsakanna
嗨,Jobi和Benny, 那真的起作用了。但问题是,所选项目的BlueHighlight仍然存在,并且它隐藏了背景颜色动画。有解决方案吗? - Amsakanna
将会有一个触发器来设置蓝色,所以你需要将其移除。请在此处粘贴您当前正在使用的 XAML。 - Jobi Joy
嗨Benny和Jobi, 这个怎么样? http://vbcity.com/blogs/xtab/archive/2009/06/28/background-color-for-wpf-listbox-selected-item.aspx希望能解决蓝色高亮问题。我今天会检查一下。 - Amsakanna
是的,这是ControlTemplate显示默认蓝色,所以您需要覆盖ControlTemplate和ItemContainerStyle来避免这种情况。 - Jobi Joy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接