使用DoubleAnimation增加Opacity属性时出错

时间:2010-07-23 06:32:32

标签: c# wpf xaml animation

我有一个ListViewItem控件的样式:

<EventTrigger RoutedEvent="ListViewItem.MouseEnter">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity"                              
                              From="0.0" To="1.0" Duration="0:0:0.5" AutoReverse="False" SpeedRatio="2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>

                <EventTrigger RoutedEvent="ListViewItem.MouseLeave">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity"                              
                              From="1.0" To="0.0" Duration="0:0:0.5" AutoReverse="False" SpeedRatio="2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>

我想当鼠标在ListViewItem上方时,项目边框缓慢出现,当鼠标离开时,边框效果消失, 但是当鼠标离开项目时我得到这个错误:

Cannot resolve all property references in the property path 'BitmapEffect.Opacity'. Verify 
that applicable objects support the properties. 

请注意,当我只使用路由到EventTrigger的第一个ListViewItem.MouseEnter时,程序运行正常!但它的观点并不好!

我正在使用OuterGlowBitmapEffect!

                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="BitmapEffect">
                        <Setter.Value>
                            <OuterGlowBitmapEffect GlowColor="SkyBlue" GlowSize="20" />
                        </Setter.Value>
                    </Setter>
                    <Setter Property="Foreground" Value="Black" />
                </Trigger>

1 个答案:

答案 0 :(得分:3)

我尝试使用BitmapEffect,它可以正常运行上面的代码。

<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
        <Setter Property="BitmapEffect">
            <Setter.Value>
                 <OuterGlowBitmapEffect GlowColor="Blue" GlowSize="5" />
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="ListViewItem.MouseEnter">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity"                               
                          From="0.0" To="1.0" Duration="0:0:0.5" AutoReverse="False" SpeedRatio="2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
            <EventTrigger RoutedEvent="ListViewItem.MouseLeave">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity"                               
                          From="1.0" To="0.0" Duration="0:0:0.5" AutoReverse="False" SpeedRatio="2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>

添加了整个样本。

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:uc="clr-namespace:WpfApplication10"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="WpfApplication10.Window1"
x:Name="Window"
Title="Window1" mc:Ignorable="d">
<Window.Resources>
    <DataTemplate x:Key="ItemTemplate1">
        <StackPanel>
            <TextBlock Text="{Binding Property1}"/>
            <Image Source="{Binding Property2}" HorizontalAlignment="Left" Height="64" Width="64"/>
        </StackPanel>
    </DataTemplate>
    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}">
        <Setter Property="BitmapEffect">
            <Setter.Value>
                 <OuterGlowBitmapEffect GlowColor="Blue" GlowSize="5" />
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <EventTrigger RoutedEvent="ListViewItem.MouseEnter">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity"                               
                          From="0.0" To="1.0" Duration="0:0:0.5" AutoReverse="False" SpeedRatio="2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
            <EventTrigger RoutedEvent="ListViewItem.MouseLeave">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="BitmapEffect.Opacity"                               
                          From="1.0" To="0.0" Duration="0:0:0.5" AutoReverse="False" SpeedRatio="2" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource SampleDataSource1}}">
    <ListBox  DataContext="{Binding Source={StaticResource SampleDataSource3}}" 
              ItemTemplate="{DynamicResource ItemTemplate1}" ItemsSource="{Binding Collection}" 
              ItemContainerStyle="{DynamicResource ListBoxItemStyle1}" >
    </ListBox>
</Grid>

相关问题