简体   繁体   English

使用DoubleAnimation设置图像不透明度

[英]Set Image Opacity with an DoubleAnimation

This should be fairly simple but I can't seem to get it right. 这应该很简单,但我似乎无法正确解决。 I have a collection of System.Windows.Controls.Image . 我有一个System.Windows.Controls.Image的集合。 When I select one of them, I would like all the others to get an opacity of 0.5 . 当我选择其中一个时,我希望所有其他的不透明度为0.5 I'm using MVVM, and the logic behind this (finding the selected image and setting it as Enabled ) is done in the ViewModel , and is working. 我正在使用MVVM,其背后的逻辑(查找选定的图像并将其设置为Enabled )在ViewModel完成,并且正在工作。 So basically this is working: 所以基本上这是可行的:

<Image Grid.Row="0" Source="{Binding ItemImage}" IsEnabled="{Binding ItemImageEnabled}">
<Image.Style>
    <Style TargetType="Image">
        <Style.Triggers>
            <Trigger Property="IsEnabled"  Value="False">
                <Setter Property="Opacity" Value="0.5"/>
            </Trigger>
            <Trigger Property="IsEnabled" Value="True">
                <Setter Property="Opacity" Value="1.0"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Image.Style>
</Image>

Now I would like the opacity shifting to be animated, fading it from 1.0 to 0.5 when the image is not selected, and fading from 0.5 to 1.0 when it is. 现在,我希望对不透明度进行动画处理,当未选择图像时,将不透明度从1.0渐变为0.5当不选择图像时,将透明度从0.5渐变为1.0 I would have thought at this would work: 我本以为这样可以工作:

<Image Grid.Row="0" Source="{Binding ItemImage}" IsEnabled="{Binding ItemImageEnabled}">
<Image.Style>
    <Style TargetType="Image">
        <Style.Triggers>
            <Trigger Property="IsEnabled"  Value="False">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0.5" BeginTime="0:0:0" Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="IsEnabled" Value="True">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.5" To="1" BeginTime="0:0:0" Duration="0:0:1"/>
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</Image.Style>

... But it doesn't ...但是没有

Anybody have any idea. 任何人都有任何想法。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks in advance. 提前致谢。

You need Trigger.ExitActions instead of two Trigger . 您需要Trigger.ExitActions而不是两个Trigger This should be what you want: 这应该是您想要的:

<Style TargetType="Image">
    <Style.Triggers>        
        <Trigger Property="IsEnabled" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.5" To="1" BeginTime="0:0:0" Duration="0:0:1"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
            <Trigger.ExitActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" From="1" To="0.5" BeginTime="0:0:0" Duration="0:0:1"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.ExitActions>
        </Trigger>
    </Style.Triggers>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM