【问题标题】:Animate selected item of wpf listbox动画 wpf 列表框的选定项
【发布时间】:2011-01-16 13:39:25
【问题描述】:

我正在尝试为我的应用程序中的所有列表框设置全局样式。下面是我使用的 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> 

【问题讨论】:

    标签: c# wpf listview selecteditem


    【解决方案1】:

    为 ListBox 创建 ItemContainerStyle 并为 ListBoxItem.IsSelected 添加触发器 == True

    【讨论】:

    • 不设置Template 属性,而是设置ItemContainerStyle 属性。使用 Jobi 所说的触发器在其中创建一个样式,然后在触发器被激活时应用您的动画。
    • 谢谢,我今天就试试,让你知道:)
    • 嗨乔比和本尼,这真的很管用。但是发生的情况是,所选项目的 BlueHighlight 仍然存在,并且它隐藏了背景颜色动画。有什么解决办法吗?
    • 会有一个设置蓝色的触发器,因此您必须将其删除。在此处粘贴当前工作的 XAML。
    • 嗨,本尼和乔比,这个怎么样? vbcity.com/blogs/xtab/archive/2009/06/28/…希望能解决 BlueHighlight 问题。今天让我检查一下
    猜你喜欢
    • 1970-01-01
    • 2020-02-18
    • 1970-01-01
    • 2014-05-14
    • 2023-04-05
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多