【问题标题】:Error while increasing Opacity property using DoubleAnimation使用 DoubleAnimation 增加不透明度属性时出错
【发布时间】:2010-07-23 06:32:31
【问题描述】:

我有一个 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 上时,item 的边框慢慢出现,当鼠标离开它时,边框效果消失, 但是当鼠标离开项目时出现此错误:

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

请注意,当我只使用路由到ListViewItem.MouseEnter 的第一个EventTrigger 时,程序可以正常工作!但是视野不好!

我正在使用 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>

【问题讨论】:

    标签: c# wpf xaml animation


    【解决方案1】:

    我正在尝试使用 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>
    

    【讨论】:

    • 感谢您的回复,但这会使整个 ListViewItem 淡出。我只想淡出并淡入 ListViewItem 的 BitmapEffect 的 Opacity 属性!我之前为 Storyboard.TargetProperty="BitmapEffect.Opacity" 尝试过这种方式,但我再次遇到同样的错误!
    • 我已经编辑了我的 XAML,请检查。不知道你用的是什么位图效果。
    • 谢谢,不过这个也报错了!我正在使用 OuterGlowBitmapEffect。
    • 我已添加示例 xaml。我正在使用 Blend 示例源数据。
    • 再次感谢您!我在单独的项目中尝试了你的代码,它工作正常,我认为我的问题是别的!我再次检查我的代码!但是很奇怪!!!
    猜你喜欢
    • 1970-01-01
    • 2014-02-21
    • 2016-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多